On Tue, 2007-10-23 at 12:15 +0100, Prof Brian Ripley wrote: > Take a look at several of the graphics functions, e.g. plot.default(), for > a good way to do this via local functions.
Dear Prof. Ripley Thank you for your solution - this works well for my real problem. This is such a simple solution that I was thrown by it and it took me a few minutes to realise how this worked - very neat! All the best, Gavin > > 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 > > > > > -- %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% 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 %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.