@@ -81,13 +81,11 @@ read_abs <- function(cat_no = NULL,
81
81
retain_files = TRUE ,
82
82
check_local = TRUE ) {
83
83
84
- if (isTRUE(check_local ) &&
84
+ # Anything other than TRUE is equivalent to FALSE
85
+ check_local <- isTRUE(check_local )
86
+ if (check_local &&
87
+ identical(tables , " all" ) &&
85
88
fst_available(cat_no = cat_no , path = path )) {
86
- if (! identical(tables , " all" )) {
87
- warning(" `tables` was provided" ,
88
- " yet `check_local = TRUE` and fst files are available " ,
89
- " so `tables` will be ignored." )
90
- }
91
89
out <- fst :: read_fst(path = catno2fst(cat_no = cat_no , path = path ))
92
90
out <- tibble :: as_tibble(out )
93
91
if (is.null(series_id )) {
@@ -133,10 +131,88 @@ read_abs <- function(cat_no = NULL,
133
131
tables <- " all"
134
132
}
135
133
136
- if (! is.logical(metadata )) {
134
+ if (! is.atomic(tables )) {
135
+ stop(" `tables` was not atomic." )
136
+ }
137
+ if (anyNA(tables )) {
138
+ warning(" `tables` contains missing values, these will be removed." )
139
+ tables <- tables [! is.na(tables )]
140
+ }
141
+ if (! is.integer(tables ) && length(tables ) != 0L ) {
142
+ if (is.character(tables )) {
143
+ if (length(tables ) != 1L ) {
144
+ stop(" `tables` was character, but had length " , length(tables ), " . " ,
145
+ ' The only valid character value for `tables` is "all".' )
146
+ }
147
+ if (tables != " all" ) {
148
+ stop(" `tables = " , tables , " `." ,
149
+ ' The only valid character value for `tables` is "all".' )
150
+ }
151
+ } else {
152
+ # Edge case: if user supplies a very large number,
153
+ # any(tables != as.integer(tables))
154
+ # below will return a cryptic error message (possibly during recursion).
155
+ # Unlikely to happen on purpose.
156
+ if (min(tables ) < 0 || max(tables ) > .Machine $ integer.max ) {
157
+ stop(" `tables` was a numeric vector but had values outside [0, .Machine$integer.max]. " ,
158
+ " These are unlikely values for table numbers and are " )
159
+ }
160
+ if (! is.numeric(tables ) || any(tables != as.integer(tables ))) {
161
+ stop(" `tables` was not an integer(ish) vector of table numbers." )
162
+ }
163
+ tables <- as.integer(tables )
164
+ }
165
+ }
166
+
167
+ if (! is.logical(metadata ) || length(metadata ) != 1L || is.na(metadata )) {
137
168
stop(" `metadata` argument must be either TRUE or FALSE" )
138
169
}
139
170
171
+ if (check_local ) {
172
+ # In the case of table = "all" we simply get the fst file for
173
+ # the whole cat_no. Equally simple is the case of a single
174
+ # table. Both are handled by length(tables <= 1L)
175
+
176
+ # If len > 1 integer vector is supplied to tables, we recurse
177
+ # for each element of tables, checking the table's fst file availability
178
+ # independently of the other elements. Either we use the fst
179
+ # file or we download that single table. Each operation of lapply
180
+ # will produce a tibble.
181
+ if (length(tables ) < = 1L ) {
182
+ if (fst_available(cat_no = cat_no , table = tables , path = path )) {
183
+ file.fst <- catno2fst(cat_no = cat_no , table = tables , path = path )
184
+ out <- fst :: read_fst(file.fst )
185
+ out <- tibble :: as_tibble(out )
186
+ if (is.null(series_id )) {
187
+ return (out )
188
+ }
189
+ if (series_id %in% out [[" series_id" ]]) {
190
+ users_series_id <- series_id
191
+ out <- dplyr :: filter(out , series_id %in% users_series_id )
192
+ } else {
193
+ warning(" `series_id` was provided," ,
194
+ " but was not present in the local table and will be ignored." )
195
+ }
196
+ return (out )
197
+ } else {
198
+ # continue as if check_local = FALSE
199
+ }
200
+ } else {
201
+ # Recursion
202
+ out <-
203
+ lapply(tables , function (ta ) {
204
+ read_abs(cat_no = cat_no ,
205
+ tables = ta ,
206
+ series_id = series_id ,
207
+ path = path ,
208
+ metadata = metadata ,
209
+ show_progress_bars = show_progress_bars ,
210
+ retain_files = retain_files )
211
+ })
212
+ return (dplyr :: bind_rows(out ))
213
+ }
214
+ }
215
+
140
216
# satisfy CRAN
141
217
ProductReleaseDate = SeriesID = NULL
142
218
@@ -252,13 +328,13 @@ read_abs <- function(cat_no = NULL,
252
328
}
253
329
254
330
# if fst is available, and what has been requested is the full data,
255
- # write the result to the <path>/ fst/ file
331
+ # or a single table, retain the fst file.
256
332
if (retain_files &&
257
- is.null(series_id ) &&
258
- identical(tables , " all" ) &&
259
- requireNamespace(" fst" , quietly = TRUE )) {
333
+ requireNamespace(" fst" , quietly = TRUE ) &&
334
+ length(tables ) < = 1L ) {
260
335
fst :: write_fst(sheet ,
261
336
catno2fst(cat_no = cat_no ,
337
+ table = tables ,
262
338
path = path ))
263
339
}
264
340
0 commit comments