Skip to content

Commit

Permalink
Fixes to .rename_columns function required
Browse files Browse the repository at this point in the history
  • Loading branch information
athowes committed Nov 14, 2024
1 parent aba7ca5 commit e08315e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
7 changes: 6 additions & 1 deletion R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
13 changes: 13 additions & 0 deletions tests/testthat/test-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
})

0 comments on commit e08315e

Please sign in to comment.