如何在 quosures 列表中插入或包含附加参数?

How do I pipe into or include an additional argument within a list of quosures?

我在下面 my_q_list 中有一个问题列表:

library(rlang)
suppressPackageStartupMessages(library(dplyr))

q_list <- function(...) {
  enquos(...)
}

my_q_list <- q_list(
  select(mpg, hp),
  filter(hp > 20),
  mutate(mpg2 = mpg*2)
)

my_q_list
#> <list_of<quosure>>
#> 
#> [[1]]
#> <quosure>
#> expr: ^select(mpg, hp)
#> env:  global
#> 
#> [[2]]
#> <quosure>
#> expr: ^filter(hp > 20)
#> env:  global
#> 
#> [[3]]
#> <quosure>
#> expr: ^mutate(mpg2 = mpg * 2)
#> env:  global

reprex package (v0.3.0)

于 2020-07-01 创建

如何使用 rlang 和 purrr,将 mtcars 数据集通过管道传输到此列表中并计算每个表达式,返回包含三个数据帧的列表?

一种方法是使用quosure算法:

purrr::map( my_q_list, ~quo( mtcars %>% !!.x ) ) %>%       # Construct desired quosures
    purrr::map( quo_squash ) %>%                           # Simplify them
    purrr::map( eval_tidy )                                # Evaluate them