expr()
and !!
anticipate the capture and unquoting of an expression. Singular. Although it could ultimately produce a select()
statement with multiple variables.
exprs()
and !!!
anticipate the capture and unquoting or one or more expressions. Plural.
library(tidyverse)
common_many <- exprs(mpg, disp:wt, starts_with("a"))
common_one_sw <- expr(starts_with("a"))
common_one_dw <- expr(disp:wt)
mtcars %>%
select(!!!common_many, gear) %>%
head(2)
#> mpg disp hp drat wt am gear
#> Mazda RX4 21 160 110 3.9 2.620 1 4
#> Mazda RX4 Wag 21 160 110 3.9 2.875 1 4
mtcars %>%
select(!!!common_one_sw, gear) %>%
head(2)
#> am gear
#> Mazda RX4 1 4
#> Mazda RX4 Wag 1 4
mtcars %>%
select(!!!common_one_dw, gear) %>%
head(2)
#> disp hp drat wt gear
#> Mazda RX4 160 110 3.9 2.620 4
#> Mazda RX4 Wag 160 110 3.9 2.875 4
mtcars %>%
select(!!common_one_sw, gear) %>%
head(2)
#> am gear
#> Mazda RX4 1 4
#> Mazda RX4 Wag 1 4
mtcars %>%
select(!!common_one_dw, gear) %>%
head(2)
#> disp hp drat wt gear
#> Mazda RX4 160 110 3.9 2.620 4
#> Mazda RX4 Wag 160 110 3.9 2.875 4
Created on 2018-04-09 by the reprex package (v0.2.0).
I used the "multiple" form (!!!
and exprs()
) on purpose, based on your example. You showed two literal expressions re: variables to include, plus one use of a select()
helper. But as you see, !!!
can also be used to unquote a single expression. I would use the most restrictive form that "should work", so your thing works, but is not too forgiving of peculiar inputs.