Gavin Simpson
2007-Oct-23 10:55 UTC
[R] passing subsets of '...' to several other functions
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, ...)
invisible(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.
G
--
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
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
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
Prof Brian Ripley
2007-Oct-23 11:15 UTC
[R] passing subsets of '...' to several other functions
Take a look at several of the graphics functions, e.g. plot.default(), for a good way to do this via local functions. On Tue, 23 Oct 2007, Gavin Simpson wrote:> 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, ...) > invisible(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. > > G > >-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595