Skip to content

Commit

Permalink
error for empty strings in replacement, add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed Sep 9, 2024
1 parent ccd6948 commit e20a89a
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 26 deletions.
20 changes: 9 additions & 11 deletions R/data_rename.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
#' `data_rename()`, `pattern` can also be a named vector. In this case, names
#' are used as values for the `replacement` argument (i.e. `pattern` can be a
#' character vector using `<new name> = "<old name>"` and argument `replacement`
#' will be ignored then). If some of the names in `pattern` are not provided,
#' column names will be removed. See 'Examples'.
#' will be ignored then).
#' @param replacement Character vector. Indicates the new name of the columns
#' selected in `pattern`. Can be `NULL` (in which case column are numbered
#' in sequential order). If not `NULL`, `pattern` and `replacement` must be
Expand Down Expand Up @@ -46,15 +45,6 @@
#'
#' # Change all
#' head(data_rename(iris, replacement = paste0("Var", 1:5)))
#'
#' # remove one column name
#' head(data_rename(
#' iris,
#' pattern = c("Sepal.Length", "Sepal.Width"),
#' replacement = c("keep_length", "")
#' ))
#' # same as
#' head(data_rename(iris, c(keep_length = "Sepal.Length", "Sepal.Width")))
#' @seealso
#' - Functions to rename stuff: [data_rename()], [data_rename_rows()], [data_addprefix()], [data_addsuffix()]
#' - Functions to reorder or remove columns: [data_reorder()], [data_relocate()], [data_remove()]
Expand Down Expand Up @@ -92,6 +82,14 @@ data_rename <- function(data,
replacement <- paste0(seq_along(pattern))
}

# coerce to character
replacement <- as.character(replacement)

# check if `replacement` has no empty strings and no NA values
if (anyNA(replacement) || !all(nzchar(replacement))) {
insight::format_error("`replacement` is not allowed to have `NA` or empty strings.")
}

# if duplicated names in replacement, append ".2", ".3", etc. to duplicates
# ex: c("foo", "foo") -> c("foo", "foo.2")
if (anyDuplicated(replacement) > 0L) {
Expand Down
12 changes: 1 addition & 11 deletions man/data_rename.Rd

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

3 changes: 1 addition & 2 deletions man/text_format.Rd

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

19 changes: 17 additions & 2 deletions tests/testthat/test-data_rename.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,26 @@ test_that("data_rename returns a data frame", {
test_that("data_rename: pattern must be of type character", {
expect_error(
data_rename(test, pattern = 1),
regexp = "Argument `pattern` must be of type character."
regexp = "Argument `pattern` must be of type character"
)
expect_error(
data_rename(test, pattern = TRUE),
regexp = "Argument `pattern` must be of type character."
regexp = "Argument `pattern` must be of type character"
)
})

test_that("data_rename: replacement not allowed to have NA or empty strings", {
expect_error(
data_rename(test, pattern = c(test = "Species", "Sepal.Length")),
regexp = "`replacement` is not allowed"
)
expect_error(
data_rename(
test,
pattern = c("Species", "Sepal.Length"),
replacement = c("foo", NA_character_)
),
regexp = "`replacement` is not allowed"
)
})

Expand Down

0 comments on commit e20a89a

Please sign in to comment.