[R] A function as argument of another function

Spencer Graves spencer.graves at pdf.com
Thu Apr 17 17:12:31 CEST 2003


What you wrote works if "fn" is a function.  If it is a character 
string, I would look at Venables and Ripley (2000) S Programming;  I 
believe they discuss several ways of doing that.

For certain applications of this nature, I have used constructs like 
"eval(pars(text= paste('...')))".

hth,
Spencer Graves

vito muggeo wrote:
> Dear all,
> I would like to write a function like:
> myfun<-function(x,fn) {xx<-exp(x); x*fn(xx)}
> where fn is a symbolic description of any function with its argument to be
> specified. Therefore
> myfun(5,"2+0.3*y^2")
> should return 5*(2+0.3*exp(5)^2),
> myfun(5,"log(y)") should return 5*log(exp(5)) and so on.
> 
> I tried with "expression" and others, but without success. How can I solve
> my problem?
> 
> Many thanks,
> vito
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help



More information about the R-help mailing list