[R] how to use "..."

Bert Gunter gunter.berton at gene.com
Thu Jan 17 16:53:54 CET 2013


Well..

On Thu, Jan 17, 2013 at 7:42 AM, Ivan Calandra
<ivan.calandra at u-bourgogne.fr> wrote:
> Ok, it is that simple... Actually I had tried it but messed up so that it
> didn't work.
> Do you know where I can find some documentation about it?

The "R language definition" manual would be the logical place to look,
no? And sure enough, it's there!

The Introduction to R tutorial also contains info in the "writing
functions" section.

A very good, though now dated, exposition can be found in V&R's S
Programming book. Although a little care is needed due to age, it's
still my favorite.

-- Bert

>
> Regarding return(), I know that it's not necessary, but when the function
> gets more complicated, I like to have it because it becomes clearer to me.
>
> Thanks all!
> Ivan
>
> --
> Ivan CALANDRA
> Université de Bourgogne
> UMR CNRS/uB 6282 Biogéosciences
> 6 Boulevard Gabriel
> 21000 Dijon, FRANCE
> +33(0)3.80.39.63.06
> ivan.calandra at u-bourgogne.fr
> http://biogeosciences.u-bourgogne.fr/calandra
>
> Le 17/01/13 15:55, R. Michael Weylandt a écrit :
>>
>> On Thu, Jan 17, 2013 at 2:36 PM, Ivan Calandra
>> <ivan.calandra at u-bourgogne.fr> wrote:
>>>
>>> Dear users,
>>>
>>> I'm trying to learn how to use the "...".
>>>
>>> I have written a function (simplified here) that uses doBy::summaryBy():
>>> # 'dat' is a data.frame from which the aggregation is computed
>>> # 'vec_cat' is a integer vector defining which columns of the data.frame
>>> should be use on the right side of the formula
>>> # 'stat_fun' is the function that will be run to aggregate
>>> stat.group <- function(dat, vec_cat, stat_fun){
>>>      require(doBy)
>>>      df <-
>>>
>>> summaryBy(as.formula(paste0(".~",paste0(names(dat)[vec_cat],collapse="+"))),
>>> data=dat, FUN=stat_fun)
>>>      return(df)
>>> }
>>> [SNIP EXAMPLE -- THANK YOU FOR IT]
>>>
>>> Now summaryBy() has an "..." argument and I would like to use it.
>>> For example, I would like to be able to add the trim argument to my call
>>> like this:
>>> stat.group(dat=my_data, vec_cat=1, stat_fun=mean, trim=0.2)
>>>
>>>
>> Thanks for the great working examples!
>>
>> It's actually not too hard here --  just pass "..." down as if it were
>> an argument and let summaryBy() do the hard work of actually handling
>> the dots:
>>
>> stat.group <- function(dat, vec_cat, stat_fun, ...){
>>      require(doBy)
>>      df <-
>> summaryBy(as.formula(paste0(".~",paste0(names(dat)[vec_cat],collapse="+"))),
>> data=dat, FUN=stat_fun, ...)
>>      return(df)
>> }
>>
>> Also, note that as a matter of style, you can actually clean this up a
>> little bit: R follows the trend of many functional languages in
>> automatically returning the value of the last expression evaluated:
>>
>> stat.group <- function(dat, vec_cat, stat_fun, ...){
>>      require(doBy)
>>
>> summaryBy(as.formula(paste0(".~",paste0(names(dat)[vec_cat],collapse="+"))),
>> data=dat, FUN=stat_fun, ...)
>> }
>>
>> Cheers,
>> Michael
>>
>
> ______________________________________________
> R-help at 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.



-- 

Bert Gunter
Genentech Nonclinical Biostatistics

Internal Contact Info:
Phone: 467-7374
Website:
http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm



More information about the R-help mailing list