Re: [R] Use of the option in functions.

From: Duncan Murdoch <>
Date: Wed, 06 Apr 2011 12:56:01 -0400

On 06/04/2011 12:04 PM, KENNETH R CABRERA wrote:
> Hi R users:
> I try this code, where "fun" is a parameter of a random generating
> function name, and I pretend to use "..." parameter to pass the parameters
> of different random generating functions.
> What am I doing wrong?
> f1<-function(nsim=20,n=10,fun=rnorm,...){
> vp<-replicate(nsim,t.test(fun(n,...),fun(n,...))$p.value)
> return(vp)
> }
> This works!
> f1()
> f1(n=20,mean=10)
> This two fails:
> f1(n=10,fun=rexp)
> f1(n=10,fun=rbeta,shape1=1,shape2=2)
> Thank you for your help.

I imagine it's a scoping problem: replicate() is probably not evaluating the ... in the context you think it is. You could debug this by writing a function like

showArgs <- function(n, ...) {


and calling f1(n=10, fun=showArgs), but it might be easier just to avoid the problem:

f1 <- function(nsim=20,n=10,fun=rnorm,...){

     localfun <- function() fun(n, ...)
     vp<-replicate(nsim,t.test(localfun(), localfun())$p.value)

} mailing list PLEASE do read the posting guide and provide commented, minimal, self-contained, reproducible code. Received on Wed 06 Apr 2011 - 16:57:39 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 06 Apr 2011 - 19:20:27 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.

list of date sections of archive