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
Pronto, It is very easy. You have to define your fn as a function: myfun <- function(x,fn) {xx<-exp(x); x*fn(xx)} testf <- function(y) 2 + 0.3*y^2 myfun(5, testf) or you may write myfun(5, function(y) 2 + 0.3*y^2) In your example you passed a character string into the function, I am pretty sure it can be transformed into function but you don't need to. Cheers, Ott | From: "vito muggeo" <vito.muggeo at giustizia.it> | Date: Thu, 17 Apr 2003 09:45:41 +0200 | | 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?
>>>>> "vito" == vito muggeo <vito.muggeo at giustizia.it> >>>>> on Thu, 17 Apr 2003 09:45:41 +0200 writes:(MM: I've added " " around "<-" and empty lines:) vito> Dear all, vito> I would like to write a function like: vito> myfun <- function(x,fn) {xx<-exp(x); x*fn(xx)} vito> where fn is a symbolic description of any function vito> with its argument to be specified. Therefore vito> myfun(5,"2+0.3*y^2") vito> should return 5*(2+0.3*exp(5)^2), vito> myfun(5,"log(y)") should return 5*log(exp(5)) and so on. vito> I tried with "expression" and others, but without vito> success. How can I solve my problem? As you say yourself in the subject, the argument must be a *function*, not a string or an expression, e.g., function(y) y^2 is a function. So, myfun <- function(x,fn) { xx <- exp(x); x*fn(xx) } myfun(5, function(y) 2+0.3*y^2) gives 33049.7, as desired. Regards, Martin Maechler <maechler at stat.math.ethz.ch> http://stat.ethz.ch/~maechler/
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 hello Vito, how about: myfun <- function(x,fn) { xx <- exp(x) aux <- function(y) { eval(parse(text=fn)) } x*aux(xx) } myfun(5,"2+0.3*y^2") as an alternative to Ott's answer. HTH, Bernhard ______________________________________________ R-help at stat.math.ethz.ch mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help ---------------------------------------------------------------------- If you have received this e-mail in error or wish to read our e-mail disclaimer statement and monitoring policy, please refer to http://www.drkw.com/disc/email/ or contact the sender.
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