[R] How to convert a list to a ... argument for a function

Duncan Murdoch murdoch.duncan at gmail.com
Tue Oct 5 20:35:11 CEST 2010


  On 05/10/2010 2:23 PM, Richard R. Liu wrote:
> Hi,
>
>
> I have a function f<- function(..., func){ something }, where func is a
> function of the form function(...).  I would like to pass func all the arguments
> passed to f except the last.  I know that I can manipulate the variable number
> of arguments passed to f by converting ... to a list, i.e., arglist<-
> list(...).  But how do I pass func the first n-1 list items of arglist (n<-
> length(arglist)), as n-1 arguments, not as one list of n-1 items?

Do you know the name of the last one?  If so, just declare it as an 
argument to f, and it won't be caught by ... .

Do you know how many arguments there are?  Then the barely documented 
..1, ..2 etc might work for you.

Otherwise, convert it to a list, and use do.call(func, args), where args 
is the list without the last element.

Duncan Murdoch



More information about the R-help mailing list