The by
argument in dplyr::left_join
(and friends) expects a list of strings. My code already defines the variables of interest as a list of quosures so I want to avoid writing the list down a second time by converting the list of quosures to a list of strings.
I went hunting around the rlang
docs but did not find an existing solution. The quo_expr
family of functions can splice and convert quosures to a single string but they do not produce a list of strings:
library(rlang)
# this works
sepal_length <- quo(Sepal.Length)
quo_text(sepal_length)
# this doesnt
sepaldims <- quos(Species, Sepal.Length)
quo_text(sepaldims)
Here's an example where this would be useful:
library(dplyr)
# group_by accepts a list of quosures
sepaldims <- quos(Species, Sepal.Length)
iris_sepal <- iris %>%
group_by(!!!sepaldims) %>%
summarize(sepal_width = mean(Sepal.Width), petal_width = mean(Petal.Width))
petaldims <- quos(!!!sepaldims, Petal.Length)
iris_petal <- iris %>%
group_by(!!!petaldims) %>%
summarize(sepal_width = mean(Sepal.Width), petal_width = mean(Petal.Width))
# but left join expects a list of strings
iris_merge <- left_join(iris_sepal, iris_petal, by = c("Species", "Sepal.Length"))