From c6b15007a1872fe0171e97dc59622d8812f0332b Mon Sep 17 00:00:00 2001 From: Daniel Date: Thu, 21 Nov 2024 22:24:22 +0100 Subject: [PATCH] add test --- R/data_rename.R | 8 ++++---- man/data_rename.Rd | 8 ++++---- tests/testthat/test-data_rename.R | 11 +++++++++++ 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/R/data_rename.R b/R/data_rename.R index 1d541882a..8e8f83421 100644 --- a/R/data_rename.R +++ b/R/data_rename.R @@ -20,9 +20,9 @@ #' vector using ` = ""` and argument `replacement` will #' be ignored then). #' @param replacement Character vector. Can be one of the following: -#' - A character vector that indicates the new name of the columns selected in -#' `pattern`. `pattern` and `replacement` must be of the same length. -#' - `NULL`, in which case column are numbered in sequential order. +#' - 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. #' Currently supported tokens are `{col}` and `{n}`. `{col}` will be replaced #' by the column name, i.e. the corresponding value in `pattern`. `{n}` will @@ -34,7 +34,7 @@ #' replacement = "new_name_from_{col}" #' ) #' ``` -#' would returns new column names `new_name_from_am` and `new_name_from__vs`. +#' would returns new column names `new_name_from_am` and `new_name_from_vs`. #' See 'Examples'. #' #' If `pattern` is a named vector, `replacement` is ignored. diff --git a/man/data_rename.Rd b/man/data_rename.Rd index 0ba4fa381..5dd11e0d9 100644 --- a/man/data_rename.Rd +++ b/man/data_rename.Rd @@ -112,9 +112,9 @@ functions (see 'Details'), this argument may be used as workaround.} \item{replacement}{Character vector. Can be one of the following: \itemize{ -\item A character vector that indicates the new name of the columns selected in -\code{pattern}. \code{pattern} and \code{replacement} must be of the same length. -\item \code{NULL}, in which case column are numbered in sequential order. +\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. 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 @@ -127,7 +127,7 @@ be replaced by the number of the variable that is replaced. For instance, ) }\if{html}{\out{}} -would returns new column names \code{new_name_from_am} and \code{new_name_from__vs}. +would returns new column names \code{new_name_from_am} and \code{new_name_from_vs}. See 'Examples'. } diff --git a/tests/testthat/test-data_rename.R b/tests/testthat/test-data_rename.R index e01c42f8b..2b6d744b3 100644 --- a/tests/testthat/test-data_rename.R +++ b/tests/testthat/test-data_rename.R @@ -140,3 +140,14 @@ test_that("data_rename preserves attributes", { expect_named(a1, names(a2)) }) + + +# curl-styled pattern -------------------------- + +test_that("data_rename curl-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")) + out <- data_rename(mtcars[1:3], c("mpg", "cyl", "disp"), "{col}_is_column_{n}") + expect_named(out, c("mpg_is_column_1", "cyl_is_column_2", "disp_is_column_3")) +})