diff --git a/R/utils.R b/R/utils.R index 7d237e4e3..abf930c19 100644 --- a/R/utils.R +++ b/R/utils.R @@ -133,7 +133,12 @@ #' @keywords internal #' @importFrom stats setNames .rename_columns <- function(df, new_names, old_names) { - are_char <- is.character(new_names) & is.character(old_names) + if (is.null(new_names) | is.null(old_names)) { + return(df) + } + new_char <- sapply(new_names, is.character) + old_char <- sapply(old_names, is.character) + are_char <- new_char & old_char valid_new_names <- new_names[are_char] valid_old_names <- old_names[are_char] if (length(are_char) > 0) { diff --git a/tests/testthat/test-utils.R b/tests/testthat/test-utils.R index 5b9701aaf..e8d0fc7cd 100644 --- a/tests/testthat/test-utils.R +++ b/tests/testthat/test-utils.R @@ -70,3 +70,16 @@ test_that(".make_intercepts_explicit does not add an intercept if the distributi attr(formula_updated$pforms$sigma, ".Environment") <- NULL expect_equal(formula, formula_updated) }) + +test_that(".rename_columns works correctly", { # nolint: line_length_linter. + df <- data.frame(a = 1, b = 2) + new_df <- .rename_columns(df, new_names = c("x", "y"), old_names = c("a", "b")) + expect_named(new_df, c("x", "y")) +}) + +test_that(".rename_columns does nothing if old_names is only NULL", { # nolint: line_length_linter. + df <- data.frame(a = 1, b = 2) + new_df <- .rename_columns(df, new_names = c("a", "b"), old_names = c(NULL, NULL)) + expect_named(new_df, c("a", "b")) +}) +