[R] create a function given a string

Peter Dalgaard BSA p.dalgaard at biostat.ku.dk
Fri Dec 15 00:12:19 CET 2000

Don Wingate <Don.Wingate at intellichem.com> writes:

> Hello,
> I am trying to dynamically create/define a function inside of another
> function (as part of a package.) 
> I build a string that looks something like this: "x + y" and what I need to
> do is define a function f <- function(x,y) { x+y }.
> This function "string" is much more complex than this example and depends on
> other variables so there is no way to predict what the function will look
> like.
> The ultimate goal is to pass this function 'outer' 
> I have tried many different approaches and just can't find a way to convert
> the string "x + y" to the function body x + y.

parse(text="x+y") ?

BTW: beware that outer wants a *vectorized* function of the two
variables not just a function of two scalars (usually fixable by an
sapply construction)

   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch

More information about the R-help mailing list