我在下面的 my_q_list 中有一个 quosures 列表:
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
Created on 2020-07-01 by the reprex package https://reprex.tidyverse.org (v0.3.0)
使用 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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)