Skip to content

Commit

Permalink
curl -> glue
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed Nov 22, 2024
1 parent 5949e7e commit 03ff678
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 15 deletions.
2 changes: 1 addition & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 10 additions & 10 deletions R/data_rename.R
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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 ",
Expand All @@ -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)) {
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions man/data_rename.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions tests/testthat/test-data_rename.R
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down

0 comments on commit 03ff678

Please sign in to comment.