Skip to content
Prev 140253 / 398506 Next

Passing (Optional) Arguments

Jason Q. McClintic wrote:
That's tricky, because ... becomes one big list.  You have a few choices:

foo1 and foo2 can gain a ... arg, and ignore it.  Then you pass ... to 
both of them.  Problem:  you can pass typos to them and won't get a 
complaint.

foo3 can gain a,b,c,x,y,z args, and pass those along to foo1 and foo2.  
Problem:  duplication, maintenance problems, etc.

You can use list(...) in foo3, and manually split the args to those that 
belong in foo1 and those that belong in foo2, and then construct calls 
from them.  (This allows you to recognize args that don't go to either 
place, and signal errors.)

You can pass foo1Args and foo2Args as lists to foo3, and construct calls 
from those, e.g.

foo3 <- function(lambda, foo1Args, foo2Args)
  lambda*do.call(foo1, foo1Args)*do.call(foo2,foo2Args)

There are other options (e.g. using local versions of foo1 and foo2 
within foo3) that  are variations on the ones above.

It's hard to give good advice on this, because whatever you do will be a 
tradeoff.  You'll see variations on all of the above in the base packages.

Duncan Murdoch