Skip to content

Commit e3c35ce

Browse files
committed
working version, passes all tests
1 parent 0a9cc82 commit e3c35ce

File tree

1 file changed

+15
-43
lines changed

1 file changed

+15
-43
lines changed

R/read.R

Lines changed: 15 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -42,64 +42,34 @@ read_cells <- function(drfile, sheet, variables, translate = FALSE, translations
4242
#' @return A named list. Values are coerced to character
4343
#' @noRd
4444
#'
45-
# THE OLD FUNCTION
46-
# read_keyvalue <- function(drfile, sheet, range, translate = FALSE, translations = NULL, atomicclass = "character", ...) {
47-
# keyvalue <- readxl::read_excel(drfile, sheet = sheet, range = range, col_names = c("key", "value"))
48-
# if (translate) {
49-
# keyvalue$key <- long_to_shortnames(keyvalue$key, translations)
50-
# }
51-
# kvlist <- lapply(keyvalue$value, coerce, atomicclass)
52-
# names(kvlist) <- keyvalue$key
53-
# kvlist
54-
# }
55-
#
56-
# THIS PART IS FROM THE OLD CALLING FUNCTION
57-
# if (!location$type == "cells") {
58-
# chunks <- lapply(location$ranges, function(range) {
59-
# read_function(drfile = drfile, sheet = location$sheet, range = range, translate = location$translate,
60-
# translations = guide$translations, atomicclass = atomicclass)
61-
# })
62-
# } else {
63-
# chunks <- read_cells(drfile = drfile, sheet = location$sheet, variables = location$variables, translate = location$translate,
64-
# translations = guide$translations, atomicclass = atomicclass)
65-
# }
66-
#
67-
# chunk <- switch(
68-
# location$type,
69-
# "keyvalue" = do.call(c, chunks),
70-
# "table" = dplyr::bind_rows(chunks),
71-
# "platedata" = suppressMessages(Reduce(dplyr::full_join, chunks)),
72-
# "cells" = chunks
73-
# )
7445
read_keyvalue <- function(drfile, sheet, ranges, translate = FALSE, translations = NULL, atomicclass = "character", ...) {
7546

76-
chunks <- lapply(ranges, function(range) {
47+
kvtable <- lapply(ranges, function(range) {
7748
readxl::read_excel(drfile, sheet = sheet, range = range, col_names = c("key", "value"))
78-
})
79-
80-
keyvalue <- do.call(c, chunks) |>
49+
}) |>
50+
dplyr::bind_rows()
8151

8252
if (translate) {
83-
keyvalue$key <- long_to_shortnames(keyvalue$key, translations)
53+
kvtable$key <- long_to_shortnames(kvtable$key, translations)
8454
}
8555

56+
kvlist <- as.list(kvtable$value)
57+
8658
if (length(atomicclass) == 1) {
87-
kvlist <- lapply(keyvalue$value, coerce, atomicclass)
59+
kvlist <- lapply(kvlist, coerce, atomicclass)
8860
} else {
89-
90-
if (!length(atomicclass) == length(keyvalue)) {
61+
if (length(atomicclass) != length(kvlist)) {
9162
rlang::abort(
9263
glue::glue("The number of atomic classes ({ length(atomicclass) }) must be 1 or equal to the
93-
number of elements ({ length(keyvalue) }) in the keyvalue table.")
64+
number of elements ({ length(kvlist) }) in the keyvalue table.")
9465
)
9566
}
96-
97-
kvlist <- lapply(seq_along(keyvalue$value), function(i) {
98-
keyvalue$value[i] |> coerce(atomicclass[i])
67+
kvlist <- lapply(seq_along(kvlist), function(i) {
68+
kvlist[[i]] |> coerce(atomicclass[i])
9969
})
10070
}
101-
names(kvlist) <- keyvalue$key
102-
kvlist
71+
72+
setNames(kvlist, kvtable$key)
10373
}
10474

10575
#' Read table formatted data from a spreadsheet
@@ -270,10 +240,12 @@ read_data <- function(drfile, guide, checkname = FALSE) {
270240
}
271241
num.template.version <- package_version(template.version)
272242
num.min.version <- package_version(guide$template.min.version)
243+
273244
if (num.template.version < num.min.version) {
274245
rlang::abort(glue::glue("The guide is incompatible with the template.
275246
The template version should be minimally {guide$template.min.version}, whereas it is {result$template.metadata$template.version}."))
276247
}
248+
277249
if (!is.null(guide$template.max.version)) {
278250
num.max.version <- package_version(guide$template.max.version)
279251
if (num.max.version < num.template.version) {

0 commit comments

Comments
 (0)