99# ' @noRd
1010# '
1111read_cells <- function (drfile , sheet , variables , translate = FALSE , translations = NULL , atomicclass = ' character' ) {
12+ # Process each variable
1213 result <- lapply(variables , function (v ) {
13- dims <- dim( cellranger :: as.cell_limits( v $ cell ))
14- if (any(dims > 1 )) {
14+ # Ensure the cell address points to a single cell
15+ if (any(dim( cellranger :: as.cell_limits( v $ cell )) > 1 )) {
1516 rlang :: abort(glue :: glue(" A cell address should point to a single cell." ))
1617 }
17- x <- suppressMessages(readxl :: read_excel(drfile , sheet = sheet , range = v $ cell , col_names = FALSE ))
18- if (nrow(x ) == 0 ) {
19- x <- NA
18+
19+ # Read the cell value
20+ cell_data <- suppressMessages(
21+ readxl :: read_excel(drfile , sheet = sheet , range = v $ cell , col_names = FALSE )
22+ )
23+
24+ # Handle empty cells
25+ if (nrow(cell_data ) == 0 ) {
26+ NA
2027 } else {
21- x <- x [[1 ]][1 ]
28+ cell_data [[1 ]][1 ]
2229 }
2330 })
24- result <- stats :: setNames(result , sapply(variables , function (x ) x $ name ))
31+
32+ # Assign names to the results
33+ result <- stats :: setNames(result , sapply(variables , `[[` , " name" ))
34+
35+ # Coerce values to the specified atomic class
2536 lapply(result , coerce , atomicclass )
2637}
2738
@@ -126,12 +137,33 @@ plate_to_df <- function(d) {
126137# ' @return A data frame in long format
127138# ' @noRd
128139read_key_plate <- function (drfile , sheet , ranges , translate = FALSE , translations = NULL , atomicclass = " character" , ... ) {
140+ # Read and convert each range to a long-format data frame
129141 chunks <- lapply(ranges , function (range ) {
130142 plate <- readxl :: read_excel(drfile , sheet = sheet , range = range ) | >
131143 plate_to_df()
132144 })
133- # TODO: handle vectors of atomicclass
134- suppressMessages(Reduce(dplyr :: full_join , chunks ))
145+
146+ # Combine all chunks into a single data frame
147+ combined <- suppressMessages(Reduce(dplyr :: full_join , chunks ))
148+
149+ # Handle vectors of atomicclass
150+ if (length(atomicclass ) == 1 ) {
151+ combined [] <- lapply(combined , coerce , atomicclass )
152+ } else {
153+ if (length(atomicclass ) != ncol(combined )) {
154+ rlang :: abort(glue :: glue(
155+ " The number of atomic classes ({length(atomicclass)}) must be 1 or equal to the number of columns ({ncol(combined)}) in the combined data frame."
156+ ))
157+ }
158+ combined [] <- Map(coerce , combined , atomicclass )
159+ }
160+
161+ # Translate column names if required
162+ if (translate ) {
163+ names(combined ) <- long_to_shortnames(names(combined ), translations )
164+ }
165+
166+ combined
135167}
136168
137169# ' Translation between long and short variable names
@@ -144,25 +176,34 @@ read_key_plate <- function(drfile, sheet, ranges, translate = FALSE, translation
144176# ' @return A vector of long or short variable names
145177# ' @export
146178long_to_shortnames <- function (v , translations ) {
179+ # Match long names to their corresponding short names
147180 positions <- match(v , translations $ long )
148181 shortnames <- translations $ short [positions ]
149- if (any (is.na(positions ))) {
182+
183+ # Handle missing translations
184+ missing_indices <- is.na(positions )
185+ if (any(missing_indices )) {
150186 rlang :: warn(" Missing translations. Using original long names." )
151- shortnames [is.na( positions ) ] <- v [is.na( positions ) ]
187+ shortnames [missing_indices ] <- v [missing_indices ]
152188 }
189+
153190 shortnames
154191}
155192
156193# ' @return A vector of long variable names
157194# ' @rdname long_to_shortnames
158195# ' @export
159196short_to_longnames <- function (v , translations ) {
197+ # Match short names to their corresponding long names
160198 positions <- match(v , translations $ short )
161199 longnames <- translations $ long [positions ]
162- if (any(is.na(positions ))) {
163- rlang :: warn(" Missing reverse translations. Using short names." )
200+
201+ # Handle missing translations
202+ if (anyNA(positions )) {
203+ rlang :: warn(" Missing reverse translations. Using original short names." )
164204 longnames [is.na(positions )] <- v [is.na(positions )]
165205 }
206+
166207 longnames
167208}
168209
0 commit comments