Skip to content

Commit

Permalink
do not use column indices as replacement
Browse files Browse the repository at this point in the history
  • Loading branch information
etiennebacher committed Nov 30, 2024
1 parent 0327135 commit e370ff1
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 11 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ BREAKING CHANGES AND DEPRECATIONS
- argument `pattern` is deprecated. Use `select` instead.
- argument `safe` is deprecated. The function now errors when `select`
contains unknown column names.
- when `replacement` is `NULL`, an error is now thrown (previously, column
indices were used as new names).

CHANGES

Expand Down
6 changes: 0 additions & 6 deletions R/data_rename.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
#' @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.
#' - `NULL`, in which case columns are numbered in sequential order.
#' - A string (i.e. character vector of length 1) with a "glue" styled
#' pattern. Currently supported tokens are:
#' - `{col}` which will be replaced by the column name, i.e. the
Expand Down Expand Up @@ -117,11 +116,6 @@ data_rename <- function(data,
replacement <- names(select)
}

# name columns 1, 2, 3 etc. if no replacement
if (is.null(replacement)) {
replacement <- paste0(seq_along(select))
}

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

Expand Down
1 change: 0 additions & 1 deletion man/data_rename.Rd

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

9 changes: 5 additions & 4 deletions tests/testthat/test-data_rename.R
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,11 @@ test_that("data_rename: replacement not allowed to have NA or empty strings", {

# replacement -------------

test_that("data_rename uses indices when no replacement", {
x <- data_rename(test, select = c("Sepal.Length", "Petal.Length"))
expect_identical(dim(test), dim(x))
expect_named(x, c("1", "Sepal.Width", "2", "Petal.Width", "Species"))
test_that("data_rename errors when no replacement", {
expect_error(
data_rename(test, select = c("Sepal.Length", "Petal.Length")),
"There are more names in `select` than in `replacement`"
)
})

test_that("data_rename errors when too many names in 'replacement'", {
Expand Down

0 comments on commit e370ff1

Please sign in to comment.