Skip to content

Commit

Permalink
address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed Sep 9, 2024
1 parent 7a873d4 commit 7142959
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
2 changes: 1 addition & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
BREAKING CHANGES

* `data_rename()` now errors when the `replacement` argument contains `NA` values
or empty strings.
or empty strings (#539).

CHANGES

Expand Down
25 changes: 19 additions & 6 deletions R/data_rename.R
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,26 @@ data_rename <- function(data,
# check if `replacement` has no empty strings and no NA values
invalid_replacement <- is.na(replacement) | !nzchar(replacement)
if (any(invalid_replacement)) {
insight::format_error(
"`replacement` is not allowed to have `NA` or empty strings.",
sprintf(
"Following names in `pattern` have no match in `replacement`: %s",
toString(pattern[invalid_replacement])
if (is.null(names(pattern))) {
# when user did not match `pattern` with `replacement`
msg <- c(
"`replacement` is not allowed to have `NA` or empty strings.",
sprintf(
"Following values in `pattern` have no match in `replacement`: %s",
toString(pattern[invalid_replacement])
)
)
)
} else {
# when user did not name all elements of `pattern`
msg <- c(
"Either name all elements of `pattern` or use `replacement`.",
sprintf(
"Following values in `pattern` were not named: %s",
toString(pattern[invalid_replacement])
)
)
}
insight::format_error(msg)
}

# if duplicated names in replacement, append ".2", ".3", etc. to duplicates
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-data_rename.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ test_that("data_rename: 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"
regexp = "Either name all elements of `pattern`"
)
expect_error(
data_rename(
Expand Down

0 comments on commit 7142959

Please sign in to comment.