I can pass a list to mutate by splicing it like in the below code:
library(tidyverse)
mtcars %>%
mutate(!!!list(a = 1, b= 2)) %>%
head()
#> mpg cyl disp hp drat wt qsec vs am gear carb a b
#> 1 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4 1 2
#> 2 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4 1 2
#> 3 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1 1 2
#> 4 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1 1 2
#> 5 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2 1 2
#> 6 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1 1 2
Created on 2019-10-13 by the reprex package (v0.3.0)
Now suppose I have this list as an expression, for example, like this list_expr <- expr(list(a = 1, b= 2)). How can I pass this list expression to mutate and get the same result
library(rlang)
list_expr <- expr(list(a = 1, b= 2))
mtcars %>% mutate(!!!list_expr)
#> Warning: Unquoting language objects with `!!!` is deprecated as of rlang 0.4.0.
#> Please use `!!` instead.
#>
#> # Bad:
#> dplyr::select(data, !!!enquo(x))
#>
#> # Good:
#> dplyr::select(data, !!enquo(x)) # Unquote single quosure
#> dplyr::select(data, !!!enquos(x)) # Splice list of quosures
#>
#> This warning is displayed once per session.
#> Error: Column `list(a = 1, b = 2)` must be length 32 (the number of rows) or one, not 2
mtcars %>% mutate(!!list_expr)
#> Error: Column `list(a = 1, b = 2)` must be length 32 (the number of rows) or one, not 2
Created on 2019-10-13 by the reprex package (v0.3.0)