Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing along ellipsis arguments to two different functions? [duplicate]

Tags:

r

How do I write a main function flexible enough to pass extra arguments along to multiple functions? Simple attempts have failed:

> mainF <- function(f1,f2,...) {
+     f2( X=f1(...), ... )
+ }
> mainF(function(x) x, function(X, y) X*y, x=5, y=3)
Error in f2(X = f1(...), ...) : unused argument (x = 5)

I can see how this might be possible by examining formals and matching call arguments in the ellipsis to the formals of each function. Is there a better way, though?

like image 672
Ari B. Friedman Avatar asked May 27 '13 14:05

Ari B. Friedman


1 Answers

You can pass a list to one of the subfunctions, ie

mainF <- function(f1, f2, ..., args.f2 = list()) {
     do.call(f2, c(X=f1(...), args.f2))
}

mainF(function(x) x, function(X, y) X*y, x=5, args.f2 = list(y=3))

(untested, but you got the gist)

like image 176
baptiste Avatar answered Sep 21 '22 15:09

baptiste