-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetch.R
272 lines (199 loc) · 8.35 KB
/
fetch.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#' @title Fetch data from many data sources
#' @encoding UTF-8
#' @description
#' The \strong{fetch} package allows you to retrieve data from many different
#' data sources. The package retrieves data in a memory-efficient manner.
#' You first identify the data by defining a data catalog. Then fetch
#' the data from the catalog. Catalogs can be defined for many popular
#' data formats: csv, rds, sas7bdat, excel, etc.
#'
#' The functions contained in the \strong{fetch} package are as follows:
#' \itemize{
#' \item{\code{\link{catalog}}: Creates a data library}
#' \item{\code{\link{fetch}}: Creates a data dictionary}
#' \item{\code{\link{import_spec}}: Defines an import spec for a specific dataset}
#' }
#' @name fetch
#' @aliases fetch-package
#' @keywords internal
"_PACKAGE"
#' @title Fetch a dataset from a data catalog
#' @encoding UTF-8
#' @description The \code{fetch} function retrieves a dataset from a data
#' catalog. The function accepts a catalog item as the first parameter. The
#' catalog item is the only required parameter. The "select" parameter allows
#' you to pull only some of the columns. The "where" and "top" parameters
#' may be used to define a subset of the data to retrieve. The "import_specs"
#' parameter accepts an \code{\link{import_spec}} object, which can be used
#' to control how data is read into the data frame.
#' @param catalog The catalog item to fetch data for. Catalog items
#' are created using the \code{\link{catalog}} function.
#' @param select A vector of column names or column numbers to extract from the
#' data item. Note that the column names can be easily obtained as a vector
#' from the catalog item, and then manipulated to suit your needs.
#' @param where An optional expression to be used to filter the fetched data.
#' Use the base R \code{\link{expression}} function to define the expression.
#' The expression allows logical operators and Base R functions. Column names
#' can be unquoted.
#' @param top A number of records to return from the head of the data item.
#' Valid value is an integer.
#' @param import_specs The import specs to use for the fetch operation. Import
#' specs can be used to control the data types of the fetched dataset.
#' An import specification is created with the \code{\link{import_spec}}
#' function. See the documentation of this function for additional details
#' and an example.
#' @return The desired dataset, returned as a tibble.
#' @seealso The \code{\link{catalog}} function to create a data catalog.
#' Also see the \code{\link{import_spec}} function to create import specifications.
#' @examples
#' # Get data directory
#' pkg <- system.file("extdata", package = "fetch")
#'
#' # Create catalog
#' ct <- catalog(pkg, engines$csv)
#'
#' # View catalog
#' ct
#' # data catalog: 6 items
#' # - Source: C:/packages/fetch/inst/extdata
#' # - Engine: csv
#' # - Items:
#' # data item 'ADAE': 56 cols 150 rows
#' # data item 'ADEX': 17 cols 348 rows
#' # data item 'ADPR': 37 cols 552 rows
#' # data item 'ADPSGA': 42 cols 695 rows
#' # data item 'ADSL': 56 cols 87 rows
#' # data item 'ADVS': 37 cols 3617 rows
#'
#' # Example 1: Fetch Entire Dataset
#'
#' # Get data from the catalog
#' dat1 <- fetch(ct$ADEX)
#'
#' # View Data
#' dat1
#' # A tibble: 348 × 17
#' # STUDYID USUBJID SUBJID SITEID TRTP TRTPN TRTA TRTAN RANDFL SAFFL
#' # <chr> <chr> <chr> <chr> <chr> <dbl> <chr> <dbl> <chr> <chr>
#' # 1 ABC ABC-01-0… 049 01 ARM D 4 ARM D 4 Y Y
#' # 2 ABC ABC-01-0… 049 01 ARM D 4 ARM D 4 Y Y
#' # 3 ABC ABC-01-0… 049 01 ARM D 4 ARM D 4 Y Y
#' # 4 ABC ABC-01-0… 049 01 ARM D 4 ARM D 4 Y Y
#' # 5 ABC ABC-01-0… 050 01 ARM B 2 ARM B 2 Y Y
#' # 6 ABC ABC-01-0… 050 01 ARM B 2 ARM B 2 Y Y
#' # 7 ABC ABC-01-0… 050 01 ARM B 2 ARM B 2 Y Y
#' # 8 ABC ABC-01-0… 050 01 ARM B 2 ARM B 2 Y Y
#' # 9 ABC ABC-01-0… 051 01 ARM A 1 ARM A 1 Y Y
#' # 10 ABC ABC-01-0… 051 01 ARM A 1 ARM A 1 Y Y
#' # 338 more rows
#' # 7 more variables: MITTFL <chr>, PPROTFL <chr>, PARAM <chr>,
#' # PARAMCD <chr>, PARAMN <dbl>, AVAL <dbl>, AVALCAT1 <chr>
#' # Use `print(n = ...)` to see more rows
#'
#' # Example 2: Fetch a Subset
#'
#' # Get data with selected columns and where expression
#' dat2 <- fetch(ct$ADEX, select = c("SUBJID", "TRTA", "RANDFL", "SAFFL"),
#' where = expression(SUBJID == '051'))
#'
#' # View Data
#' dat2
#' # A tibble: 4 x 4
#' # SUBJID TRTA RANDFL SAFFL
#' # <chr> <chr> <chr> <chr>
#' # 1 051 ARM A Y Y
#' # 2 051 ARM A Y Y
#' # 3 051 ARM A Y Y
#' # 4 051 ARM A Y Y
#'
#' @export
fetch <- function(catalog, select = NULL, where = NULL, top = NULL, import_specs = NULL) {
ret <- NULL
if ("dinfo" %in% class(catalog)) {
ret <- load_data(catalog, where, top, import_specs, select)
} else {
stop("Function requires a data catalog item as input.")
}
return(ret)
}
#' @import tibble
load_data <- function(dinfo, where = NULL, top = NULL, import_specs = NULL,
select = NULL) {
# Get the file list according to the engine type
if (!"dinfo" %in% class(dinfo))
stop("Class must by 'dinfo'")
dat <- NULL
eng <- attr(dinfo, "engine")
pth <- attr(dinfo, "path")
nm <- attr(dinfo, "name")
fl <- attr(dinfo, "where")
if (is.null(import_specs)) {
spc <- attr(dinfo, "import_specs")
if (!is.null(spc))
import_specs <- spc
}
# Combine filters if necessary
if (!is.null(fl)) {
if (!is.null(where)) {
where <- str2expression(paste(fl, "&",
as.character(where), collapse = ""))
} else {
where <- str2expression(fl)
}
}
if (eng == engines$csv) {
dat <- get_data_csv(pth, nm, where = where, top = top,
import_specs = import_specs)
} else if (eng == engines$rds) {
dat <- get_data_rds(pth, nm, where = where, top = top,
import_specs = import_specs)
} else if (eng %in% c(engines$rdata, engines$rda)) {
dat <- get_data_rda(pth, nm, where = where, top = top,
import_specs = import_specs)
} else if (eng == engines$sas7bdat) {
dat <- get_data_sas7bdat(pth, nm, where = where, top = top,
import_specs = import_specs)
} else if (eng == engines$dbf) {
dat <- get_data_dbf(pth, nm, where = where, top = top,
import_specs = import_specs)
} else if (eng == engines$xpt) {
dat <- get_data_xpt(pth, nm, where = where, top = top,
import_specs = import_specs)
} else if (eng == engines$xlsx) {
dat <- get_data_xlsx(pth, nm, where = where, top = top,
import_specs = import_specs)
} else if (eng == engines$xls) {
dat <- get_data_xls(pth, nm, where = where, top = top,
import_specs = import_specs)
}
# Deal with select parameter
if (!is.null(select)) {
nms <- names(dat)
if (typeof(select) == "character") {
snms <- select
if (all(snms %in% nms)) {
dat <- dat[ , snms]
} else {
rnms <- c()
for (nm in snms) {
if (!nm %in% nms)
rnms[length(rnms) + 1] <- nm
}
stop(paste0("Select parameter names not found in data: ",
paste(rnms, collapse = " ")))
}
} else if (typeof(select) %in% c("integer", "numeric")) {
snms <- nms[select]
if (any(is.na(snms))) {
pos <- select[is.na(snms)]
stop(paste0("Select parameter positions not found in data: ",
paste(pos, collapse = " ")))
} else {
dat <- dat[ , snms]
}
} else {
stop("Select parameter type invalid.")
}
}
return(dat)
}