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

喜你入骨 提交于 2021-01-27 11:57:35

问题


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

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

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?


回答1:


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


来源:https://stackoverflow.com/questions/62689612/how-do-i-pipe-into-or-include-an-additional-argument-within-a-list-of-quosures

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!