List of quosures as input of a set of functions

209 views Asked by At

This question refers to "Programming with dplyr"

I want to slice the ... argument of a function and use each element as an argument for a corresponding function.

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

should evaluate for example foo(x, y, z) in this form:

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

so that x, y, z remain quoted till they get evaluated in bar.

I tried this:

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

I have two intentions:

  1. Learn better how tidyeval/rlang works and when to use it.
  2. turn future::futureOf() into a function that get me more then one futures at once.

This approach might be overly complicated, because I don't fully understand the underlying concepts of tidyeval yet.

1

There are 1 answers

0
G. Grothendieck On BEST ANSWER

You don't really need any packages for this. match.call can be used.

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)

giving:

[[1]]
bar(x = 1)

[[2]]
bar(y = 2)

[[3]]
bar(z = 3)

Another test

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

giving:

[[1]]
[1] 1

[[2]]
[1] 4

[[3]]
[1] 9