I’ve a function f() that has some named parameters. It calls a function g() and I want to pass all f’s parameters to it. Is this possible?
Using … just covers the extra arguments:
f=function(a,callback,b,c,d,...){
z=a-b
callback(z,...)
}
g=function(z,...){
print(list(...)) #Only shows $e
print(z) #-1
print(a,b,c,d) #'a' not found
}
f(1,g,2,3,d=4,e=5);
I thought formals() was the answer, but it just seems to be argument names, not their values!
f=function(a,callback,b,c,d,...){
z=a-b
callback(z,formals())
}
g=function(z,...){
args=list(...)[[1]]
print(args$a) #(no output)
print(class(args$a)) #"name"
}
f(1,g,2,3,d=4,e=5);
Is it possible? Thanks.
Well, something like this is certainly possible. You should just figure our for yourself in which frame / point you’d like to evaluate the arguments of f which are then forwarded to g.
The typical procedure consists of match.call() call inside f to actually record the call expression which f was called with, then changing the call expression as it should be convenient for you (e.g. filtering out unnecessary args, adding new, etc.) and then evaluation of the new call expression via eval() call. So, something like this should (almost) work: