I'm creating a workflow that contains the same piping steps of renaming, selecting by, then mutating all using a name I provide prior to the pipe.
I have had success using enquo()
and !!
(bang bang) to rename to my desired string and then select it again, but when I reach the mutate step it either repeats the text string as the column values or will not evaluate.
I've recreated the code below:
#Testing rename, select, and mutate use cases for enquo()
#Load packages
library(dplyr)
library(rlang)
library(magrittr)
#Create name I want to pass
new_var <- quo("new_name")
#Create Test Data
t1 <- tibble(Year = c(2000:2004),
old_name = c(NA, 1, 1, 1, NA))
I can rename the column with quo_name()
and :=
t1 %>%
rename( !! quo_name(new_var) := old_name)
I can select on it using !!
t1 %>%
rename( !! quo_name(new_var) := old_name) %>%
select(Year, !!new_var)
But I can't then call that column in mutate and use the values
t1 %>%
rename( !! quo_name(new_var) := old_name) %>%
select(Year, !!new_var) %>%
mutate(test_var = (!! new_var))