[R] create a function given a string

John Fox jfox at mcmaster.ca
Fri Dec 15 03:02:58 CET 2000


At 01:38 PM 14/12/2000 -0800, you wrote:
>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.
>
>Any suggestions would be appreciated.

If I follow this correctly, then you know the names of the arguments, but 
not the statement that comprises the body of the function. How about 
something like this?

f<-function(x,y) eval(parse(text="x+y"))

I hope that this helps,
  John



________________________________
  John Fox
  Department of Sociology
  McMaster University
  email: jfox at McMaster.ca
  web: www.socsci.mcmaster.ca/jfox
________________________________

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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