From f6bec45465e7024ef52cd8ae6b2853ff3fd7b6dd Mon Sep 17 00:00:00 2001 From: Daniel Date: Fri, 13 Dec 2024 09:00:34 +0100 Subject: [PATCH] `data_rename()`: "Error: Following variable(s) were not found" in version '0.13.0.17' Fixes #571 --- R/data_rename.R | 24 +++++++++++++----------- tests/testthat/test-data_rename.R | 10 ++++++++++ 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/R/data_rename.R b/R/data_rename.R index eb2415d67..f1f158bcc 100644 --- a/R/data_rename.R +++ b/R/data_rename.R @@ -103,17 +103,19 @@ 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 - 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 - this only works for data frames + 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" + ) + } # Forbid partially named "select", # Ex: if select = c("foo" = "Species", "Sepal.Length") then the 2nd name and diff --git a/tests/testthat/test-data_rename.R b/tests/testthat/test-data_rename.R index 3495a3355..8ee09c0ab 100644 --- a/tests/testthat/test-data_rename.R +++ b/tests/testthat/test-data_rename.R @@ -221,3 +221,13 @@ test_that("Argument `pattern` is deprecated", { fixed = TRUE ) }) + +test_that("works with lists", { + result <- list( + 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_identical(names(out), new_names) +})