@@ -143,7 +143,6 @@ read_glatos_workbook <- function(
143143 wb_version = NULL ,
144144 simplify = TRUE
145145) {
146-
147146 # See version-specific file specifications
148147 # internal glatos_workbook_spec in R/sysdata.r
149148
@@ -156,7 +155,6 @@ read_glatos_workbook <- function(
156155
157156 # -Workbook v1.3, v1.4--------------------------------------------------------------
158157 if (wb_version %in% c(" 1.3" , " 1.4" )) {
159-
160158 # Get sheet names in external file
161159 wb_sheets <- readxl :: excel_sheets(wb_file )
162160
@@ -167,26 +165,38 @@ read_glatos_workbook <- function(
167165 } else {
168166 wb_sheets [tolower(wb_sheets ) %in% names(glatos_workbook_schema $ v1.3 )]
169167 }
170-
168+
171169 # Identify extra (project-specific) sheets
172- extra_sheets <- setdiff(tolower(sheets_to_read ),
173- names(glatos_workbook_schema $ v1.3 ))
174-
170+ extra_sheets <- setdiff(
171+ tolower(sheets_to_read ),
172+ names(glatos_workbook_schema $ v1.3 )
173+ )
174+
175175 # Check that extra sheet names do not match any standard sheet names
176- invalid_sheet_names <-
177- if (simplify ){ intersect(extra_sheets ,
178- c(" metadata" ,
179- " animals" ,
180- " receivers" ))
181- } else { NULL }
182-
183- if (length(invalid_sheet_names ) > 0 ) stop(" When `read_all = TRUE` and " ,
184- " `simplify = TRUE`, " ,
185- " a project specific sheet cannot " ,
186- " be named 'metadata', 'animals' " ,
187- " or 'receivers'." ,
188- call. = FALSE )
189-
176+ invalid_sheet_names <-
177+ if (simplify ) {
178+ intersect(
179+ extra_sheets ,
180+ c(
181+ " metadata" ,
182+ " animals" ,
183+ " receivers"
184+ )
185+ )
186+ } else {
187+ NULL
188+ }
189+
190+ if (length(invalid_sheet_names ) > 0 ) {
191+ stop(" When `read_all = TRUE` and " ,
192+ " `simplify = TRUE`, " ,
193+ " a project specific sheet cannot " ,
194+ " be named 'metadata', 'animals' " ,
195+ " or 'receivers'." ,
196+ call. = FALSE
197+ )
198+ }
199+
190200
191201 # Preallocate glatos_workbook object
192202 wb <- stats :: setNames(
@@ -205,10 +215,9 @@ read_glatos_workbook <- function(
205215 value = TRUE ,
206216 invert = TRUE
207217 )
208-
218+
209219
210220 for (i in 1 : length(sheets_to_read )) {
211-
212221 schema_i <- glatos_workbook_schema [[" v1.3" ]][[tolower(sheets_to_read [i ])]]
213222
214223 # Specify first row to read (with headers)
@@ -229,13 +238,17 @@ read_glatos_workbook <- function(
229238 readxl :: read_excel(
230239 wb_file ,
231240 sheet = sheets_to_read [i ],
232- range = if (extra_sheet_i ) NULL else readxl :: cell_limits(
233- ul = c(xl_first_row , 1 ),
234- lr = c(NA , xl_last_col )
235- ),
236- col_types = if (extra_sheet_i ) NULL else " list" ,
241+ range = if (extra_sheet_i ) {
242+ NULL
243+ } else {
244+ readxl :: cell_limits(
245+ ul = c(xl_first_row , 1 ),
246+ lr = c(NA , xl_last_col )
247+ )
248+ },
249+ col_types = if (extra_sheet_i ) NULL else " list" ,
237250 na = c(" " , " NA" ),
238- # n_max = 0,
251+ # n_max = 0,
239252 guess_max = 1048576 ,
240253 .name_repair = " minimal"
241254 ),
@@ -294,30 +307,29 @@ read_glatos_workbook <- function(
294307 )
295308 }
296309
297-
310+
298311 # Check and cast standard columns in standard sheets
299-
300- if (tolower(sheets_to_read [i ]) %in% names(glatos_workbook_schema $ v1.3 )){
301312
313+ if (tolower(sheets_to_read [i ]) %in% names(glatos_workbook_schema $ v1.3 )) {
302314 # Preallocate new object for parsed/cast values
303315 # keep as tibble here to preserve structure
304316 sheet_i2 <- sheet_i []
305-
317+
306318 # Add attribute for warnings and errors
307319 # warnings are warning_cast_to_check
308320 # errors are error_input_class_skipped and error_cast_failed
309-
321+
310322 attr(sheet_i2 , " warning_cast_to_check" ) <- list ()
311323 attr(sheet_i2 , " error_input_class_skipped" ) <- list ()
312324 attr(sheet_i2 , " error_cast_failed" ) <- list ()
313-
314-
325+
326+
315327 # Coerce by expected column type
316-
328+
317329 # character
318330 char_cols <- col_names_i [tolower(col_names_i ) %in%
319331 with(schema_i , name [type == " character" ])]
320-
332+
321333 for (j in char_cols ) {
322334 sheet_i2 [[j ]] <-
323335 if (nrow(sheet_i ) > 0 ) {
@@ -328,12 +340,12 @@ read_glatos_workbook <- function(
328340 as.character(sheet_i [[j ]])
329341 }
330342 }
331-
332-
343+
344+
333345 # numeric
334346 num_cols <- col_names_i [tolower(col_names_i ) %in%
335347 with(schema_i , name [type == " numeric" ])]
336-
348+
337349 for (j in num_cols ) {
338350 sheet_i2 [[j ]] <-
339351 if (nrow(sheet_i ) > 0 ) {
@@ -344,16 +356,16 @@ read_glatos_workbook <- function(
344356 as.numeric(sheet_i [[j ]])
345357 }
346358 }
347-
348-
359+
360+
349361 # POSIXct
350-
362+
351363 # Only support POSIXct or character string that parses correctly
352364 # Do not accept numeric input.
353-
365+
354366 posix_cols <- col_names_i [tolower(col_names_i ) %in%
355367 with(schema_i , name [type == " POSIXct" ])]
356-
368+
357369 for (j in posix_cols ) {
358370 # cast existing POSIXct or character to character
359371 sheet_i2 [[j ]] <-
@@ -365,26 +377,26 @@ read_glatos_workbook <- function(
365377 } else {
366378 as.character(sheet_i [[j ]])
367379 }
368-
369-
380+
381+
370382 # cast character to POSIXct, enforce timezone, but return UTC
371-
383+
372384 args_ij <- schema_i $ args [schema_i $ name == tolower(j )]
373-
385+
374386 # strip spaces (for formatting consistency)
375387 args_ij <- gsub(" " , " " , args_ij )
376-
388+
377389 if (grepl(" tz=REFCOL" , args_ij )) {
378390 tz_col <- gsub(" tz=REFCOL\\ (|\\ )" , " " , args_ij )
379-
391+
380392 tz_ij <- paste0(" US/" , sheet_i [[grep(tz_col ,
381393 names(sheet_i ),
382394 ignore.case = TRUE
383395 )]])
384396 } else {
385397 tz_ij <- gsub(" tz=|tz=\" |\" " , " " , args_ij )
386398 }
387-
399+
388400 sheet_i2 [[j ]] <-
389401 if (nrow(sheet_i ) > 0 ) {
390402 cast(sheet_i2 [[j ]],
@@ -398,19 +410,19 @@ read_glatos_workbook <- function(
398410 } else {
399411 as.POSIXct(NA , tz = tz_ij )[0 ]
400412 }
401-
413+
402414 attr(sheet_i2 [[j ]], " tzone" ) <- " UTC"
403415 } # end j
404-
405-
416+
417+
406418 # Date
407-
419+
408420 # Only support POSIXct or character string that parses correctly
409421 # Do not accept numeric input.
410-
422+
411423 date_cols <- col_names_i [tolower(col_names_i ) %in%
412424 with(schema_i , name [type == " Date" ])]
413-
425+
414426 for (j in date_cols ) {
415427 # cast existing POSIXct or character to character
416428 sheet_i2 [[j ]] <-
@@ -423,13 +435,13 @@ read_glatos_workbook <- function(
423435 as.Date(NA )[0 ]
424436 }
425437 } # end j
426-
427-
438+
439+
428440 # Handle 'extra' columns (not in schema)
429441 # if multiple classes present in a column, cast to "highest-level" class
430-
442+
431443 extra_cols <- col_names_i [! (tolower(col_names_i ) %in% schema_i $ name )]
432-
444+
433445 if (read_all ) {
434446 supported_classes <- c(
435447 " POSIXct" ,
@@ -438,16 +450,16 @@ read_glatos_workbook <- function(
438450 " character" ,
439451 " logical"
440452 )
441-
453+
442454 for (j in extra_cols ) {
443455 types_ij <- unique(unlist(lapply(sheet_i [[j ]], class )))
444-
456+
445457 # expect 'highest-level' observed class
446458 type_exp <- intersect(supported_classes , types_ij )[1 ]
447-
459+
448460 # cast to type_exp
449461 # but if type_exp is POSIXct, cast to character
450-
462+
451463 sheet_i2 [[j ]] <-
452464 if (nrow(sheet_i2 ) > 0 ) {
453465 cast(sheet_i [[j ]],
@@ -464,21 +476,17 @@ read_glatos_workbook <- function(
464476 } else {
465477 std_names_i <- names(sheet_i2 )[tolower(names(sheet_i2 )) %in%
466478 schema_i $ name ]
467-
479+
468480 sheet_i2 <- sheet_i2 [, std_names_i ]
469481 }
470482
471483 # Append to wb
472484 wb [[tolower(sheets_to_read [i ])]] <- as.data.frame(sheet_i2 )
473-
474-
475485 } else {
476-
477486 # Append to wb
478487 wb [[tolower(sheets_to_read [i ])]] <- as.data.frame(sheet_i )
479-
480488 }
481-
489+
482490
483491 if (simplify ) {
484492 # Change names of all standard columns to lowercase
0 commit comments