diff --git a/R/select_nse.R b/R/select_nse.R index a6101375e..d6fc82e01 100644 --- a/R/select_nse.R +++ b/R/select_nse.R @@ -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." + )) + } } } diff --git a/tests/testthat/test-data_select.R b/tests/testthat/test-data_select.R index 1f34cb6fd..beb2c324f 100644 --- a/tests/testthat/test-data_select.R +++ b/tests/testthat/test-data_select.R @@ -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" + ) })