如何在 quosures 列表中输入或包含附加参数? [英] How do I pipe into or include an additional argument within a list of quosures?

查看:40
本文介绍了如何在 quosures 列表中输入或包含附加参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在下面的 my_q_list 中有一个 quosures 列表:

I have a list of quosures in my_q_list below:

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 包 (v0.3.0) 于 2020 年 7 月 1 日创建

Created on 2020-07-01 by the reprex package (v0.3.0)

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

Using rlang and purrr, how do I pipe the mtcars dataset into this list and evaluate each expression, returning a list of three data frames?

推荐答案

一种方法是使用 quosure 算法:

One approach is to use quosure arithmetic:

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

这篇关于如何在 quosures 列表中输入或包含附加参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆