88# ' @return A named list
99# ' @noRd
1010# '
11- read_cells <- function (drfile , sheet , variables , translate = FALSE , translations = NULL , atomicclass = ' character' ) {
11+ read_cells <- function (
12+ drfile ,
13+ sheet ,
14+ variables ,
15+ translate = FALSE ,
16+ translations = NULL ,
17+ atomicclass = ' character'
18+ ) {
1219 # Process each variable
1320 result <- lapply(variables , function (v ) {
1421 # Ensure the cell address points to a single cell
@@ -18,7 +25,12 @@ read_cells <- function(drfile, sheet, variables, translate = FALSE, translations
1825
1926 # Read the cell value
2027 cell_data <- suppressMessages(
21- readxl :: read_excel(drfile , sheet = sheet , range = v $ cell , col_names = FALSE )
28+ readxl :: read_excel(
29+ drfile ,
30+ sheet = sheet ,
31+ range = v $ cell ,
32+ col_names = FALSE
33+ )
2234 )
2335
2436 # Handle empty cells
@@ -53,11 +65,25 @@ read_cells <- function(drfile, sheet, variables, translate = FALSE, translations
5365# ' @return A named list. Values are coerced to character
5466# ' @noRd
5567# '
56- read_keyvalue <- function (drfile , sheet , ranges , translate = FALSE , translations = NULL , atomicclass = " character" , ... ) {
68+ read_keyvalue <- function (
69+ drfile ,
70+ sheet ,
71+ ranges ,
72+ translate = FALSE ,
73+ translations = NULL ,
74+ atomicclass = " character" ,
75+ ...
76+ ) {
5777 # Read and combine key-value pairs from the specified ranges
5878 kvtable <- lapply(ranges , function (range ) {
59- readxl :: read_excel(drfile , sheet = sheet , range = range , col_names = c(" key" , " value" ))
60- }) | > dplyr :: bind_rows()
79+ readxl :: read_excel(
80+ drfile ,
81+ sheet = sheet ,
82+ range = range ,
83+ col_names = c(" key" , " value" )
84+ )
85+ }) | >
86+ dplyr :: bind_rows()
6187
6288 # Translate keys if required
6389 if (translate ) {
@@ -88,18 +114,29 @@ read_keyvalue <- function(drfile, sheet, ranges, translate = FALSE, translations
88114# ' @return A data frame in long format
89115# ' @noRd
90116# '
91- read_table <- function (drfile , sheet , ranges , translate = FALSE , translations = NULL , atomicclass = " character" , ... ) {
117+ read_table <- function (
118+ drfile ,
119+ sheet ,
120+ ranges ,
121+ translate = FALSE ,
122+ translations = NULL ,
123+ atomicclass = " character" ,
124+ ...
125+ ) {
92126 # Read and combine data from the specified ranges
93127 tbl <- lapply(ranges , function (range ) {
94128 readxl :: read_excel(drfile , sheet = sheet , range = range )
95- }) | > dplyr :: bind_rows()
129+ }) | >
130+ dplyr :: bind_rows()
96131
97132 # Coerce columns to the specified atomic class
98133 if (length(atomicclass ) == 1 ) {
99134 tbl [] <- lapply(tbl , coerce , atomicclass )
100135 } else {
101136 if (length(atomicclass ) != ncol(tbl )) {
102- rlang :: abort(" The number of atomic classes must be 1 or equal to the number of columns in the table." )
137+ rlang :: abort(
138+ " The number of atomic classes must be 1 or equal to the number of columns in the table."
139+ )
103140 }
104141 tbl [] <- Map(coerce , tbl , atomicclass )
105142 }
@@ -136,11 +173,20 @@ plate_to_df <- function(d) {
136173# ' @inherit read_keyvalue
137174# ' @return A data frame in long format
138175# ' @noRd
139- read_key_plate <- function (drfile , sheet , ranges , translate = FALSE , translations = NULL , atomicclass = " character" , ... ) {
176+ read_key_plate <- function (
177+ drfile ,
178+ sheet ,
179+ ranges ,
180+ translate = FALSE ,
181+ translations = NULL ,
182+ atomicclass = " character" ,
183+ ...
184+ ) {
140185 # Read and convert each range to a long-format data frame
141186 chunks <- lapply(ranges , function (range ) {
142187 plate <- readxl :: read_excel(drfile , sheet = sheet , range = range ) | >
143188 plate_to_df()
189+ plate
144190 })
145191
146192 # Combine all chunks into a single data frame
@@ -184,11 +230,24 @@ gentranslator <- function(type = 'long-short') {
184230 function (v , translations ) {
185231 matchdf <- data.frame (v )
186232 names(matchdf ) <- col_from
187- matches <- dplyr :: left_join(matchdf , translations , by = {{col_from }})
233+ matches <- dplyr :: left_join(matchdf , translations , by = {{ col_from }})
188234 if (any(is.na(matches [[col_to ]]))) {
189- missing_translations <- paste0(" '" , matches [[col_from ]][is.na(matches [[col_to ]])], " '" , collapse = " , " )
190- rlang :: warn(c(glue :: glue(" Missing translations for: {missing_translations}." ), " i" = glue :: glue(" Will use original {col_from} names." )), use_cli_format = TRUE )
191- matches [[col_to ]][is.na(matches [[col_to ]])] <- matches [[col_from ]][is.na(matches [[col_to ]])]
235+ missing_translations <- paste0(
236+ " '" ,
237+ matches [[col_from ]][is.na(matches [[col_to ]])],
238+ " '" ,
239+ collapse = " , "
240+ )
241+ rlang :: warn(
242+ c(
243+ glue :: glue(" Missing translations for: {missing_translations}." ),
244+ " i" = glue :: glue(" Will use original {col_from} names." )
245+ ),
246+ use_cli_format = TRUE
247+ )
248+ matches [[col_to ]][is.na(matches [[col_to ]])] <- matches [[
249+ col_from
250+ ]][is.na(matches [[col_to ]])]
192251 }
193252 return (matches [[col_to ]])
194253 }
@@ -254,7 +313,11 @@ read_data <- function(drfile, guide, checkname = FALSE) {
254313 )
255314
256315 # The default atomic class is "character"
257- atomicclass <- if (! is.null(location $ atomicclass )) location $ atomicclass else " character"
316+ atomicclass <- if (! is.null(location $ atomicclass )) {
317+ location $ atomicclass
318+ } else {
319+ " character"
320+ }
258321
259322 # Read data using the appropriate function
260323 chunk <- if (location $ type == " cells" ) {
@@ -300,12 +363,16 @@ read_data <- function(drfile, guide, checkname = FALSE) {
300363 }
301364 }
302365 } else {
303- rlang :: abort(" Variable '.template$version' not found under cells or keyvalue variables" )
366+ rlang :: abort(
367+ " Variable '.template$version' not found under cells or keyvalue variables"
368+ )
304369 }
305370 }
306371
307372 # Check template name if required
308- if (checkname && guide $ template.name != result $ template.metadata $ template.name ) {
373+ if (
374+ checkname && guide $ template.name != result $ template.metadata $ template.name
375+ ) {
309376 rlang :: abort(glue :: glue(
310377 " The name of the guide ({guide$template.name}) does not match the name of the excel template ({result$template.metadata$template.name})."
311378 ))
@@ -332,7 +399,9 @@ combine_results <- function(existing, chunk, type) {
332399 " table" = dplyr :: bind_rows(existing , chunk ),
333400 " platedata" = suppressMessages(dplyr :: full_join(existing , chunk )),
334401 " cells" = c(existing , chunk ),
335- rlang :: abort(glue :: glue(" Unsupported location type for combining results: {type}" ))
402+ rlang :: abort(glue :: glue(
403+ " Unsupported location type for combining results: {type}"
404+ ))
336405 )
337406}
338407
0 commit comments