quosures 列表作为一组函数的输入

List of quosures as input of a set of functions

此题参考"Programming with dplyr"

我想对函数的 ... 参数进行切片,并将每个元素用作相应函数的参数。

foo <- function(...){
<some code>
}

应该以这种形式评估例如 foo(x, y, z)

list(bar(~x), bar(~y), bar(~z))

以便 x, y, z 保持引用直到它们在 bar 中被评估。

我试过这个:

foo <- function(...){
  arguments <- quos(...)
  out <- map(arguments, ~bar(UQ(.)))
  out
}

我有两个意图:

  1. 更好地了解 tidyeval/rlang 的工作原理以及何时使用它。
  2. future::futureOf() 变成一个函数,让我一次获得不止一个期货。

这种方法可能过于复杂,因为我还没有完全理解 tidyeval 的基本概念。

你真的不需要任何包。 match.call可以用

foo <- function(..., envir = parent.frame()) {
   cl <- match.call()
   cl$envir <- NULL
   cl[[1L]] <- as.name("bar")
   lapply(seq_along(cl)[-1], function(i) eval(cl[c(1L, i)], envir))
}

# test    
bar <- function(...) match.call()
foo(x = 1, y = 2, z = 3)

给予:

[[1]]
bar(x = 1)

[[2]]
bar(y = 2)

[[3]]
bar(z = 3)

另一个测试

bar <- function(...) ..1^2
foo(x = 1, y = 2, z = 3)

给予:

[[1]]
[1] 1

[[2]]
[1] 4

[[3]]
[1] 9