I am looking a way for easy list
constructing based on R
's tidyeval framework as defined in the rlang
package.
Below is what I want to achieve:
a <- "item_name"
b <- "item_value"
identical(
list(!!a := !!b), # list(!!a := b) is of course also fine
list(item_name = "item_value")
)
What I can obtain at the moment is:
list(!!a := !!b)
# output
[[1]]
`:=`(!(!a), !(!b)
Alternatively it can get perhaps a little bit better when adding quosure:
quo(list(!!a := !!b))
# output
<quosure: global>
~list(`:=`("item_name", "item_value"))
Unfortunately I have no idea how to proceed further from here.
In other words I would like to have a similar effect like what we can get in the dplyr
package:
transmute(iris, !!a := b)
# first few rows
Sepal.Length Sepal.Width Petal.Length Petal.Width Species item_name
1 5.1 3.5 1.4 0.2 setosa item_value
2 4.9 3.0 1.4 0.2 setosa item_value
3 4.7 3.2 1.3 0.2 setosa item_value
4 4.6 3.1 1.5 0.2 setosa item_value
5 5.0 3.6 1.4 0.2 setosa item_value
6 5.4 3.9 1.7 0.4 setosa item_value
You can use
rlang::list2()
which supports name-unquoting with:=
and splicing with!!!
.Note that you shouldn't unquote the argument itself since
list2()
is not a quoting function, it is just likelist()
with a few more syntactic features: