Skip to content

Commit f325ca4

Browse files
committed
match does not preserve original order of matched elements. using left_join instead
1 parent 6b2065f commit f325ca4

File tree

1 file changed

+56
-28
lines changed

1 file changed

+56
-28
lines changed

R/read.R

Lines changed: 56 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,31 @@ read_key_plate <- function(drfile, sheet, ranges, translate = FALSE, translation
169169
combined
170170
}
171171

172+
#' Translation function generator
173+
#' @noRd
174+
gentranslator <- function(type = 'long-short') {
175+
stopifnot(type %in% c('long-short', 'short-long'))
176+
if (type == 'long-short') {
177+
col_from = 'long'
178+
col_to = 'short'
179+
} else {
180+
col_from = 'short'
181+
col_to = 'long'
182+
}
183+
function(v, translations) {
184+
matchdf <- data.frame(v)
185+
names(matchdf) <- col_from
186+
matches <- dplyr::left_join(matchdf, translations)
187+
if (any(is.na(matches[[col_to]]))) {
188+
missing_translations <- paste0("'", matches[[col_from]][is.na(matches[[col_to]])], "'", collapse=", ")
189+
rlang::warn(glue::glue("Missing translations for: {missing_translations}."))
190+
rlang::warn(glue::glue("Will use original {col_from} names."))
191+
matches[[col_to]][is.na(matches[[col_to]])] <- matches[[col_from]][is.na(matches[[col_to]])]
192+
}
193+
return(matches[[col_to]])
194+
}
195+
}
196+
172197
#' Translation between long and short variable names
173198
#'
174199
#' @description
@@ -178,39 +203,42 @@ read_key_plate <- function(drfile, sheet, ranges, translate = FALSE, translation
178203
#' @param translations A named vector with long variable names as names and short variable names as values
179204
#' @return A vector of long or short variable names
180205
#' @export
181-
long_to_shortnames <- function(v, translations) {
182-
# Match long names to their corresponding short names
183-
positions <- match(v, translations$long)
184-
shortnames <- translations$short[positions]
185-
186-
# Handle missing translations
187-
missing_indices <- is.na(positions)
188-
if (any(missing_indices)) {
189-
missing_translations <- paste0(translations$long[missing_indices], collapse=", ")
190-
rlang::warn(glue::glue("Missing translations: {missing_translations}."))
191-
rlang::warn("Will use original long names.")
192-
shortnames[missing_indices] <- v[missing_indices]
193-
}
194-
195-
shortnames
196-
}
206+
# long_to_shortnames <- function(v, translations) {
207+
# # Match long names to their corresponding short names
208+
# positions <- match(v, translations$long)
209+
# shortnames <- translations$short[positions]
210+
#
211+
# # Handle missing translations
212+
# missing_indices <- is.na(positions)
213+
# if (any(missing_indices)) {
214+
# missing_translations <- paste0("'", translations$long[missing_indices], "'", collapse=", ")
215+
# rlang::warn(glue::glue("Missing translations: {missing_translations}."))
216+
# rlang::warn("Will use original long names.")
217+
# shortnames[missing_indices] <- v[missing_indices]
218+
# }
219+
#
220+
# shortnames
221+
# }
222+
long_to_shortnames <- gentranslator('long-short')
197223

198224
#' @return A vector of long variable names
199225
#' @rdname long_to_shortnames
200226
#' @export
201-
short_to_longnames <- function(v, translations) {
202-
# Match short names to their corresponding long names
203-
positions <- match(v, translations$short)
204-
longnames <- translations$long[positions]
205-
206-
# Handle missing translations
207-
if (anyNA(positions)) {
208-
rlang::warn("Missing reverse translations. Using original short names.")
209-
longnames[is.na(positions)] <- v[is.na(positions)]
210-
}
227+
# short_to_longnames <- function(v, translations) {
228+
# # Match short names to their corresponding long names
229+
# positions <- match(v, translations$short)
230+
# longnames <- translations$long[positions]
231+
#
232+
# # Handle missing translations
233+
# if (anyNA(positions)) {
234+
# rlang::warn("Missing reverse translations. Using original short names.")
235+
# longnames[is.na(positions)] <- v[is.na(positions)]
236+
# }
237+
#
238+
# longnames
239+
# }
240+
short_to_longnames <- gentranslator('short-long')
211241

212-
longnames
213-
}
214242

215243
#' Read all data from a spreadsheet
216244
#'

0 commit comments

Comments
 (0)