I have a function that applies to a dataframe, with the exception of some columns. These columns can be filled using tidyselection.
For example, consider this function:
rename_upper = function(df, except=NULL){
except = names(select(df, {{except}}))
rename_with(df, ~ifelse(.x %in% except, .x, toupper(.x)))
}
rename_upper(iris, except=3:5) %>% names()
#> [1] "SEPAL.LENGTH" "SEPAL.WIDTH" "Petal.Length" "Petal.Width" "Species"
rename_upper(iris, except=starts_with("Pet")) %>% names()
#> [1] "SEPAL.LENGTH" "SEPAL.WIDTH" "Petal.Length" "Petal.Width" "SPECIES"
The output is good, but I don't want it to throw an error if except
contains a column that doesn't exist in df
.
For instance, rename_upper(iris, except=6)
throws an error as location 6 doesn't exist.
Using any_of()
is unfortunately not a solution because it would restrict except
to be a character vector and I couldn't use tidyselection anymore.
Can I have the best of both worlds?