Re: [R] Passing variable arguments to functions

From: Prof Brian Ripley <ripley_at_stats.ox.ac.uk>
Date: Fri 20 Jan 2006 - 21:43:04 EST

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

______________________________________________
R-help@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
Received on Fri Jan 20 21:51:01 2006

This archive was generated by hypermail 2.1.8 : Sat 21 Jan 2006 - 02:11:00 EST