Skip to content
Back to formatted view

Raw Message

Message-ID: <200304170802.h3H82Cu10395@punik.econ.au.dk>
Date: 2003-04-17T08:02:12Z
From: Ott Toomet
Subject: A function as argument of another function
In-Reply-To: <003a01c304b5$5f397aa0$5c13070a@it.giustizia.it> (vito.muggeo@giustizia.it)

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?