From 03ff678788252222e621b55ef98559521e2b4837 Mon Sep 17 00:00:00 2001 From: Daniel Date: Fri, 22 Nov 2024 08:21:37 +0100 Subject: [PATCH] curl -> glue --- NEWS.md | 2 +- R/data_rename.R | 20 ++++++++++---------- man/data_rename.Rd | 4 ++-- tests/testthat/test-data_rename.R | 4 ++-- 4 files changed, 15 insertions(+), 15 deletions(-) diff --git a/NEWS.md b/NEWS.md index 54b02a8ab..15b7f853d 100644 --- a/NEWS.md +++ b/NEWS.md @@ -22,7 +22,7 @@ CHANGES * `data_modify()` now recognizes `n()`, for example to create an index for data groups with `1:n()` (#535). -* The `replacement` argument in `data_rename()` now supports curl-styled +* The `replacement` argument in `data_rename()` now supports glue-styled tokens (#563). BUG FIXES diff --git a/R/data_rename.R b/R/data_rename.R index d5fb2ccd0..9823c34ba 100644 --- a/R/data_rename.R +++ b/R/data_rename.R @@ -23,7 +23,7 @@ #' - A character vector that indicates the new names of the columns selected #' in `pattern`. `pattern` and `replacement` must be of the same length. #' - `NULL`, in which case columns are numbered in sequential order. -#' - A string (i.e. character vector of length 1) with a "curl" styled pattern. +#' - A string (i.e. character vector of length 1) with a "glue" styled pattern. #' Currently supported tokens are `{col}` and `{n}`. `{col}` will be replaced #' by the column name, i.e. the corresponding value in `pattern`. `{n}` will #' be replaced by the number of the variable that is replaced. For instance, @@ -62,7 +62,7 @@ #' # Change all #' head(data_rename(iris, replacement = paste0("Var", 1:5))) #' -#' # Use curl-styled patterns +#' # Use glue-styled patterns #' head(data_rename(mtcars[1:3], c("mpg", "cyl", "disp"), "formerly_{col}")) #' head(data_rename(mtcars[1:3], c("mpg", "cyl", "disp"), "{col}_is_column_{n}")) #' @seealso @@ -146,8 +146,8 @@ data_rename <- function(data, } } - # check if we have "curl" styled replacement-string - curl_style <- length(replacement) == 1 && + # check if we have "glue" styled replacement-string + glue_style <- length(replacement) == 1 && grepl("{", replacement, fixed = TRUE) && length(pattern) > 1 @@ -158,7 +158,7 @@ data_rename <- function(data, length(replacement) - length(pattern), " names of `replacement` are not used." ) ) - } else if (length(replacement) < length(pattern) && verbose && !curl_style) { + } else if (length(replacement) < length(pattern) && verbose && !glue_style) { insight::format_alert( paste0( "There are more names in `pattern` than in `replacement`. The last ", @@ -167,9 +167,9 @@ data_rename <- function(data, ) } - # if we have curl-styled replacement-string, create replacement pattern now - if (curl_style) { - replacement <- .curl_replacement(pattern, replacement) + # if we have glue-styled replacement-string, create replacement pattern now + if (glue_style) { + replacement <- .glue_replacement(pattern, replacement) } for (i in seq_along(pattern)) { @@ -201,8 +201,8 @@ data_rename <- function(data, } -.curl_replacement <- function(pattern, replacement) { - # this function replaces "curl" tokens into their related +.glue_replacement <- function(pattern, replacement) { + # this function replaces "glue" tokens into their related # real names/values. Currently, following tokens are accepted: # - {col}: replacement is the name of the column (inidcated in "pattern") # - {n}: replacement is the number of the variable out of n, that should be renamed diff --git a/man/data_rename.Rd b/man/data_rename.Rd index 54c79f7ef..96bdf6501 100644 --- a/man/data_rename.Rd +++ b/man/data_rename.Rd @@ -115,7 +115,7 @@ functions (see 'Details'), this argument may be used as workaround.} \item A character vector that indicates the new names of the columns selected in \code{pattern}. \code{pattern} and \code{replacement} must be of the same length. \item \code{NULL}, in which case columns are numbered in sequential order. -\item A string (i.e. character vector of length 1) with a "curl" styled pattern. +\item A string (i.e. character vector of length 1) with a "glue" styled pattern. Currently supported tokens are \code{{col}} and \code{{n}}. \code{{col}} will be replaced by the column name, i.e. the corresponding value in \code{pattern}. \code{{n}} will be replaced by the number of the variable that is replaced. For instance, @@ -170,7 +170,7 @@ head(data_rename(iris, NULL)) # Change all head(data_rename(iris, replacement = paste0("Var", 1:5))) -# Use curl-styled patterns +# Use glue-styled patterns head(data_rename(mtcars[1:3], c("mpg", "cyl", "disp"), "formerly_{col}")) head(data_rename(mtcars[1:3], c("mpg", "cyl", "disp"), "{col}_is_column_{n}")) } diff --git a/tests/testthat/test-data_rename.R b/tests/testthat/test-data_rename.R index 2b6d744b3..c5bb286e4 100644 --- a/tests/testthat/test-data_rename.R +++ b/tests/testthat/test-data_rename.R @@ -142,9 +142,9 @@ test_that("data_rename preserves attributes", { }) -# curl-styled pattern -------------------------- +# glue-styled pattern -------------------------- -test_that("data_rename curl-style", { +test_that("data_rename glue-style", { data(mtcars) out <- data_rename(mtcars[1:3], c("mpg", "cyl", "disp"), "formerly_{col}") expect_named(out, c("formerly_mpg", "formerly_cyl", "formerly_disp"))