[R] passing subsets of '...' to several other functions

Gavin Simpson gavin.simpson at ucl.ac.uk
Tue Oct 23 12:55:32 CEST 2007

Dear List,

Say I have a function foo() that accepts a varying number of arguments.
This function does some plotting so I want '...' to be able to accept
graphics parameters to be passed to plot() or points() say, but '...'
should also accept optional arguments for function bar(), called from
within foo(). I don't want to hard code some extra arguments into the
formal arguments of foo(). What is a good way to proceed?

This little example might show you what I want to achieve:

foo <- function(x, arg1 = 1:10, ...)
   X <- bar(x = x, choices = arg1, ...)
   plot(X, ...)
bar <- function(x, choices, other = 20:30, another = 80:90, ...)
   x[c(choices, other)]

Using this as so:

foo(rnorm(100), other = 50:70, another = 30:40, 
    pch = 3, cex = 3, col = "red")

generates lots of warnings because 'other' and 'another' are not
recognised graphical parameters.

I can grab ... and look at what arguments are passed via ... in the call
to foo() and check if any of the arguments I actually want to look for
have been specified. I'm not sure how then to proceed, to pass only
those arguments to bar() and the rest to plot(). In my example, I'd want
to strip 'other' and 'another' from '...' and pass them as arguments to
bar() and pass the remaining graphical parameters on to plot().

One option would be to build up the function calls to bar() and plot()
using eval(parse(paste( .... ))), but as fortune("Lumley") says, I
should probably rethink this.

I searched the R-Help archives but most posts I found there were
interested in grabbing arguments in '...' and doing one thing or another
depending on what argument were called. I didn't see anything that
related to subsetting arguments in '...' and subsequently passing the
subsets on to different functions via '...' also.

Thanks in advance.


 Gavin Simpson                 [t] +44 (0)20 7679 0522
 ECRC, UCL Geography,          [f] +44 (0)20 7679 0565
 Pearson Building,             [e] gavin.simpsonATNOSPAMucl.ac.uk
 Gower Street, London          [w] http://www.ucl.ac.uk/~ucfagls/
 UK. WC1E 6BT.                 [w] http://www.freshwaters.org.uk

More information about the R-help mailing list