[R] protecting "..."

Johannes Hüsing johannes at huesing.name
Sat Nov 17 23:04:14 CET 2007


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:

> 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
-- 
Johannes Hüsing               There is something fascinating about science. 
                              One gets such wholesale returns of conjecture 
mailto:johannes at huesing.name  from such a trifling investment of fact.                
http://derwisch.wikidot.com         (Mark Twain, "Life on the Mississippi")



More information about the R-help mailing list