Skip to content

Commit

Permalink
no lists
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed Dec 13, 2024
1 parent 7ee80ca commit 094190c
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 31 deletions.
34 changes: 16 additions & 18 deletions R/data_rename.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#' possible pipe-workflow.
#'
#' @inheritParams extract_column_names
#' @param data A data frame, or an object that can be coerced to a data frame.
#' @param data A data frame.
#' @param replacement Character vector. Can be one of the following:
#' - A character vector that indicates the new names of the columns selected
#' in `select`. `select` and `replacement` must be of the same length.
Expand Down Expand Up @@ -48,10 +48,6 @@
#' `select` can also be a named character vector. In this case, the names are
#' used to rename the columns in the output data frame. See 'Examples'.
#'
#' `data_rename()` also works with list-inputs, but in this case, `select` must
#' be a character vector with valid names. It is not possible to use
#' select-helpers when `data` is a list.
#'
#' @return A modified data frame.
#'
#' @examples
Expand Down Expand Up @@ -94,6 +90,10 @@ data_rename <- function(data,
verbose = TRUE,
pattern = NULL,
...) {
# check for valid input
if (!is.data.frame(data)) {
insight::format_error("Argument `data` must be a data frame.")
}
# If the user does data_rename(iris, pattern = "Sepal.Length", "length"),
# then "length" is matched to select by position while it's the replacement
# => do the switch manually
Expand All @@ -108,19 +108,17 @@ data_rename <- function(data,
insight::format_warning("In `data_rename()`, argument `safe` is no longer used and will be removed in a future release.") # nolint
}

# change all names if no pattern specified - this does not work for lists!
if (!is.list(data) || is.data.frame(data)) {
select <- .select_nse(
select,
data,
exclude = NULL,
ignore_case = NULL,
regex = NULL,
allow_rename = TRUE,
verbose = verbose,
ifnotfound = "error"
)
}
# change all names if no pattern specified
select <- .select_nse(
select,
data,
exclude = NULL,
ignore_case = NULL,
regex = NULL,
allow_rename = TRUE,
verbose = verbose,
ifnotfound = "error"
)

# Forbid partially named "select",
# Ex: if select = c("foo" = "Species", "Sepal.Length") then the 2nd name and
Expand Down
2 changes: 1 addition & 1 deletion man/data_partition.Rd

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

6 changes: 1 addition & 5 deletions man/data_rename.Rd

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

4 changes: 0 additions & 4 deletions man/rescale.Rd

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

7 changes: 4 additions & 3 deletions tests/testthat/test-data_rename.R
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,8 @@ test_that("works with lists", {
setosa.a = structure(data.frame(1)),
versicolor.a = structure(data.frame(2))
)
new_names <- c("setosa, a", "versicolor, a")
out <- data_rename(result, select = names(result), replacement = new_names)
expect_named(out, new_names)
expect_error(
data_rename(result, select = names(result), replacement = c("a", "b")),
regex = "must be a data frame"
)
})

0 comments on commit 094190c

Please sign in to comment.