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. Thank you, Don Wingate -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._