I found a way to use
tidyeval to allow renaming columns with a named vector or a named list. I think it is quite elegant and quite obvious when written down, but it took me a little while to get my head around it, as it uses the new tidyeval paradigm. And as I do not have a blog or anything, I thought that I would post it here in case anyone is interested.
library(rlang) library(dplyr) iris <- as_tibble(iris) named_vector <- c('id' = 'Species', 'sepal' = 'Sepal.Length') iris %>% rename(!!! named_vector) %>% head(2) # A tibble: 2 x 5 sepal Sepal.Width Petal.Length Petal.Width id <dbl> <dbl> <dbl> <dbl> <fctr> 1 5.1 3.5 1.4 0.2 setosa 2 4.9 3.0 1.4 0.2 setosa named_list <- list('id' = 'Species', 'sepal' = 'Sepal.Length') # If you want to select and rename at the same time, you can use select the same way iris %>% select(!!! named_vector) %>% head(2) # A tibble: 2 x 2 id sepal <fctr> <dbl> 1 setosa 5.1 2 setosa 4.9
I think something of this kind could be included in the examples of
dplyr::rename because it is a use case that is not directly related to programming with dplyr. I think a user looking for that feature shouldn't have to go through the programming with dplyr vignette.
So my question is, should I make an issue to ask for it to be included in the examples? Or should I make a PR to propose it myself directly? Are those kind of PRs welcomed?