Search code examples
rdplyrrlangtidyeval

select and rename stored in variable


I have several similar data frames with many columns in common. I would like to select and rename a subset of those columns from any table.

library(tidyverse)
mtcars %>% 
select(my_mpg = mpg, 
       cylinders = cyl,
       gear)

Is it possible to do something like

my_select_rename <- c("my_mpg"="mpg","cylinders"="cyl","gear")

mtcars %>% 
   select_(.dots = my_select_rename)

but using the tidyeval framework instead?


Solution

  • lionel's answer to this question group_by by a vector of characters using tidy evaluation semantics provides the answer

    mtcars %>% 
        select(!!! rlang::syms(my_select_rename))