Skip to content

Commit

Permalink
allow alias
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed Nov 24, 2024
1 parent 03ff678 commit 4c34433
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
15 changes: 11 additions & 4 deletions R/data_rename.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@
#' 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 "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,
#' Currently supported tokens are `{col}` (or `{name}`) 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,
#' ```r
#' data_rename(
#' mtcars,
Expand Down Expand Up @@ -204,7 +205,7 @@ data_rename <- function(data,
.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")
# - {col}/{name}: replacement is the name of the column (indicated in "pattern")
# - {n}: replacement is the number of the variable out of n, that should be renamed
out <- rep_len("", length(pattern))
for (i in seq_along(out)) {
Expand All @@ -217,6 +218,12 @@ data_rename <- function(data,
replacement = paste0("\\1", column_name, "\\3"),
x = out[i]
)
# alias of {col} is {name}
out[i] <- gsub(
"(.*)(\\{name\\})(.*)",
replacement = paste0("\\1", column_name, "\\3"),
x = out[i]
)
# replace second accepted token
out[i] <- gsub(
"(.*)(\\{n\\})(.*)",
Expand Down
7 changes: 4 additions & 3 deletions man/data_rename.Rd

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

0 comments on commit 4c34433

Please sign in to comment.