Skip to content

Commit

Permalink
data_rename() doesn't accept empty replacement anymore (#539)
Browse files Browse the repository at this point in the history
* Update argument

* docs

* error for empty strings in `replacement`, add tests

* more informative msg

* news

* address comments
  • Loading branch information
strengejacke authored Sep 11, 2024
1 parent 0d30d24 commit a1fe5f8
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 11 deletions.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: datawizard
Title: Easy Data Wrangling and Statistical Transformations
Version: 0.12.3.2
Version: 0.12.3.3
Authors@R: c(
person("Indrajeet", "Patil", , "[email protected]", role = "aut",
comment = c(ORCID = "0000-0003-1995-6531", Twitter = "@patilindrajeets")),
Expand Down Expand Up @@ -76,3 +76,4 @@ RoxygenNote: 7.3.2
Config/testthat/edition: 3
Config/testthat/parallel: true
Config/Needs/website: easystats/easystatstemplate
Remotes: easystats/insight
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# datawizard (development)

BREAKING CHANGES

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

CHANGES

* The `pattern` argument in `data_rename()` can also be a named vector. In this
Expand Down
34 changes: 31 additions & 3 deletions R/data_rename.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@
#' string, which will be added as prefix or suffix to the column names. For
#' `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>"`).
#' character vector using `<new name> = "<old name>"` and argument `replacement`
#' 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
#' of the same length.
#' of the same length. If `pattern` is a named vector, `replacement` is ignored.
#' @param rows Vector of row names.
#' @param safe Do not throw error if for instance the variable to be
#' renamed/removed doesn't exist.
Expand All @@ -44,7 +45,6 @@
#'
#' # Change all
#' head(data_rename(iris, replacement = paste0("Var", 1:5)))
#'
#' @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 @@ -82,6 +82,34 @@ 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
invalid_replacement <- is.na(replacement) | !nzchar(replacement)
if (any(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
# ex: c("foo", "foo") -> c("foo", "foo.2")
if (anyDuplicated(replacement) > 0L) {
Expand Down
2 changes: 1 addition & 1 deletion R/standardize.models.R
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ standardize.default <- function(x,

## ---- STANDARDIZE! ----

w <- insight::get_weights(x, na_rm = TRUE)
w <- insight::get_weights(x, remove_na = TRUE)

data_std <- standardize(data[do_standardize],
robust = robust,
Expand Down
6 changes: 3 additions & 3 deletions man/data_rename.Rd

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

3 changes: 2 additions & 1 deletion 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 = "Either name all elements of `pattern`"
)
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 a1fe5f8

Please sign in to comment.