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

Prof Brian Ripley ripley at stats.ox.ac.uk
Tue Oct 23 13:15:15 CEST 2007


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



More information about the R-help mailing list