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?
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With