[R] Convert an expression to a function

wg2f at mtmail.mtsu.edu wg2f at mtmail.mtsu.edu
Mon Aug 2 05:01:52 CEST 2010


Hi John,

Here is my code practicing. Please give me some advises. Thank you.

Wu Gong

# Extract the function string 
f.str <- sub("y~","",exprtext)
# Get arglist from the text
sp1 <- paste("\\",c(getGroupMembers(Arith),"(",")"),sep="")
sp2 <- getGroupMembers(Math)
sps <- paste(c(sp1,sp2),sep="",collapse="|")
agl <- gsub("^[0123456789]+$","",strsplit(f.str,sps)[[1]]) 
agl.u <- unique(agl[agl != "" & agl != "pi"])
# The command string making the function 
c.str <- paste("f <- function(",paste(agl.u,sep="",collapse=", "),") ",f.str,sep="")
eval(parse(text=c.str))
f



More information about the R-help mailing list