[R] Variable number of arguments to function

Prof Brian Ripley ripley at stats.ox.ac.uk
Thu May 25 19:36:59 CEST 2006


On Thu, 25 May 2006, Michael Prager wrote:

> Dear R List,
>
> I have used ... to pass a varying number of arguments to a function.
> The function takes parts of specialized data objects (they happen to be
> lists) and makes comparative graphs and analyses.  The code shell looks
> like this:
>
> models.compare <- function(...) {
>   ## Get the data objects passed
>   models <- list(...)
>   nmodels <- length(models)
>   ## Get names of models:
>   modelnames <- as.character(substitute(c(...))[-1])
>   ##### much code omitted here #####
> }
>
>
> Now I would like to pass a few named arguments to the function as well,
> to control how it prints, plots, makes files.  I am seeking advice on
> the simplest way to do this (along with the varying number of data
> arguments).

Add them after ... to the argument list and always specify the full 
argument name when calling models.compare.

For R examples see .C, cbind, max, sum ....

-- 
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