Skip to content

Commit

Permalink
error on duplicated names
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed Jun 7, 2024
1 parent 84cc103 commit 41daef1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
8 changes: 8 additions & 0 deletions R/select_nse.R
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,14 @@
# after inclusion and exclusion, the original values in "select"
# may have changed, so we check that we only add names of valid values
out <- stats::setNames(out, names(new_names)[new_names %in% out])
# check if we have any duplicated names, and if so, give an error
if (anyDuplicated(names(out)) > 0) {
insight::format_error(paste0(
"Following names were duplicated after renaming: ",
text_concatenate(names(out)[duplicated(names(out))], enclose = "`"),
". Using duplicated names is no good practice and therefore discouraged. Please provide unique names."
))
}
}
}

Expand Down
9 changes: 9 additions & 0 deletions tests/testthat/test-data_select.R
Original file line number Diff line number Diff line change
Expand Up @@ -427,4 +427,13 @@ test_that("data_select renames variables on the fly", {
data_select(mtcars, c(new = "mpg", "cyl", hoho = "wt"), exclude = "wt"),
c("new", "cyl")
)
# error when names are not unique
expect_error(
data_select(mtcars, c(new = "mpg", old = "cyl", new = "wt")), # nolint
regex = "Following names were duplicated"
)
expect_error(
data_select(mtcars, c(new = "mpg", "cyl", cyl = "wt")), # nolint
regex = "Following names were duplicated"
)
})

0 comments on commit 41daef1

Please sign in to comment.