[R] protecting "..."

Duncan Murdoch murdoch at stats.uwo.ca
Sat Nov 17 23:56:52 CET 2007


On 17/11/2007 5:04 PM, Johannes Hüsing wrote:
> Dear expRts,
> I just realized that the "..." argument in a function cannot be used without
> taking precautions sometimes. The following behaviour is what I stumbled 
> upon:

The problem is the way replicate is implemented.  It takes the 
expression, and wraps it in a function(...) before calling sapply.  The 
... in your expression matches the ... in the wrapper function, not the 
... from the parent.

I think you'll need to make calls to sapply directly to do what you want.

Duncan Murdoch

> 
>> myrepl <- function(length, fun, ...) {
> + replicate(length, fun(...))}
>> myrepl(20, sample, 1:5)
>  [1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
>> myotherrepl <- function(length, fun, ...) {
> + args <- list(...)
> + replicate(length, do.call(fun, args))}
>> myotherrepl(20, sample, 1:5)
>      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14]
> [1,]    1    2    3    4    3    1    5    4    1     3     1     1     5     2
> [2,]    5    4    1    1    1    2    4    3    5     4     2     3     4     3
> [3,]    3    3    2    2    2    5    2    5    2     2     5     2     3     5
> [4,]    4    5    5    3    5    4    1    1    3     5     4     4     1     4
> [5,]    2    1    4    5    4    3    3    2    4     1     3     5     2     1
>      [,15] [,16] [,17] [,18] [,19] [,20]
> [1,]     4     5     1     2     5     5
> [2,]     2     1     4     1     1     4
> [3,]     3     3     2     3     3     3
> [4,]     1     4     3     5     4     2
> [5,]     5     2     5     4     2     1
> 
> Is the necessity to protect "..." unique to replicate(), or have I been 
> (or am I still) missing something basic here?
> 
> Best wishes
> 
> 
> Johannes
> 
> 
> ------------------------------------------------------------------------
> 
> ______________________________________________
> 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.



More information about the R-help mailing list