[R] Passing variable arguments to functions
Prof Brian Ripley
ripley at stats.ox.ac.uk
Fri Jan 20 11:43:04 CET 2006
?do.call is a good place to start. If parms is a vector, then
y <- do.call(func, as.list(parms))
should do the job. (And if parms is a list, your code probably does not
do what you want.)
For such ideas, see `S Programming' (referenced in the FAQ).
On Fri, 20 Jan 2006, Ernesto Adorio wrote:
> Hi,
>
> Is there another way to pass arguments via a vector to arbitrary functions
> as in the following code example without using a series of if else
> statements?
>
> f <- test(func, x, parms, fargs1, fargs2, ...)
> {
> # parms is a vector of parameters to func.
> # ... is for use by f, not by func.
> n <- length(parms)
> if (n == 0) y <- func(x)
> else if (n == 1) y <- func(x, parms[1])
> else if (n == 2) y <- func(x, parms[1], parms[2])
>
> # remaining body of f follows.
> }
>
> Regards,
>
> Ernie
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>
--
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