Skip to content

Commit 3cce1ce

Browse files
committed
re-write translation functions
1 parent a7c3185 commit 3cce1ce

File tree

3 files changed

+5
-35
lines changed

3 files changed

+5
-35
lines changed

R/read.R

Lines changed: 4 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ read_key_plate <- function(drfile, sheet, ranges, translate = FALSE, translation
170170
}
171171

172172
#' Translation function generator
173+
#' This function generates either the function `long_to_shortnames` or `short_to_longnames`
173174
#' @noRd
174175
gentranslator <- function(type = 'long-short') {
175176
stopifnot(type %in% c('long-short', 'short-long'))
@@ -183,11 +184,10 @@ gentranslator <- function(type = 'long-short') {
183184
function(v, translations) {
184185
matchdf <- data.frame(v)
185186
names(matchdf) <- col_from
186-
matches <- dplyr::left_join(matchdf, translations)
187+
matches <- dplyr::left_join(matchdf, translations, by = {{col_from}})
187188
if (any(is.na(matches[[col_to]]))) {
188189
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."))
190+
rlang::warn(c(glue::glue("Missing translations for: {missing_translations}."), "i" = glue::glue("Will use original {col_from} names.")), use_cli_format = TRUE)
191191
matches[[col_to]][is.na(matches[[col_to]])] <- matches[[col_from]][is.na(matches[[col_to]])]
192192
}
193193
return(matches[[col_to]])
@@ -200,43 +200,14 @@ gentranslator <- function(type = 'long-short') {
200200
#' Translate between long and short variable names. If a translation is missing the original
201201
#' variable long or short variable name from `v` is returned.
202202
#' @param v A vector of variable names
203-
#' @param translations A named vector with long variable names as names and short variable names as values
203+
#' @param translations A table of translations with columns `long` and `short`
204204
#' @return A vector of long or short variable names
205205
#' @export
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-
# }
222206
long_to_shortnames <- gentranslator('long-short')
223207

224208
#' @return A vector of long variable names
225209
#' @rdname long_to_shortnames
226210
#' @export
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-
# }
240211
short_to_longnames <- gentranslator('short-long')
241212

242213

man/excelDataGuide-package.Rd

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/long_to_shortnames.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)