Hy all, Is there a direct way to build the complete function call of an arbitrary function? Here's what I want to do. A function will build a function which will itself call a probability density function for some law given in argument to the first function:> f("gamma", 1000)will return, say, function(x, shape, rate, scale = 1/rate) dgamma(x + 1000, shape, rate, scale = 1/rate) (Notice that the arguments of the output function are those of dgamma().) I tried all sorts of combinations of call(), formals(), args() et al. to no avail. But then, I avoided, so far, to build the whole thing as a character string. Would it be the only option? Thanks for any help. -- Vincent Goulet, Professeur agr?g? ?cole d'actuariat Universit? Laval, Qu?bec Vincent.Goulet at act.ulaval.ca http://vgoulet.act.ulaval.ca
On 9/17/2006 12:36 PM, Vincent Goulet wrote:> Hy all, > > Is there a direct way to build the complete function call of an arbitrary > function? > > Here's what I want to do. A function will build a function which will itself > call a probability density function for some law given in argument to the > first function: > >> f("gamma", 1000) > > will return, say, > > function(x, shape, rate, scale = 1/rate) > dgamma(x + 1000, shape, rate, scale = 1/rate) > > (Notice that the arguments of the output function are those of dgamma().) > > I tried all sorts of combinations of call(), formals(), args() et al. to no > avail. But then, I avoided, so far, to build the whole thing as a character > string. Would it be the only option?No, do.call is what you want. dgamma(x + 1000, shape, rate, scale = 1/rate) is the same as do.call("dgamma", list(x+1000, shape, rate, scale=1/rate)) But since you're going to have to look up the parameters that are appropriate to your target density (i.e. shape, rate, scale), I'm not sure how useful this will be. It might be easier just to code the call to dgamma directly. Duncan Murdoch
Try this. We use do.call to call f with the args defined by ... except that we replace the first arg with ..1+a (where ..1 means first arg in R): F <- function(f, a) function(...) do.call(match.fun(f), replace(list(...), 1, ..1 + a)) g <- F("+", 1000) g(1,2) # 1003 On 9/17/06, Vincent Goulet <vincent.goulet at act.ulaval.ca> wrote:> Hy all, > > Is there a direct way to build the complete function call of an arbitrary > function? > > Here's what I want to do. A function will build a function which will itself > call a probability density function for some law given in argument to the > first function: > > > f("gamma", 1000) > > will return, say, > > function(x, shape, rate, scale = 1/rate) > dgamma(x + 1000, shape, rate, scale = 1/rate) > > (Notice that the arguments of the output function are those of dgamma().) > > I tried all sorts of combinations of call(), formals(), args() et al. to no > avail. But then, I avoided, so far, to build the whole thing as a character > string. Would it be the only option? > > Thanks for any help. > > -- > Vincent Goulet, Professeur agr?g? > ?cole d'actuariat > Universit? Laval, Qu?bec > Vincent.Goulet at act.ulaval.ca http://vgoulet.act.ulaval.ca > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >