[R] from character string to function body?

Atte Tenkanen attenka at utu.fi
Sat Jul 7 17:41:09 CEST 2007


Dear R users,

I wonder if it is possible to form a function from a character string. Here is an example:


> x=3
> `-`(`+`(`^`(x,3),`^`(x,2)),1) # Here is my function evaluated.
[1] 35

> V=list("`-`","(","`+`","(","`^`","(","x",",",3,")",",","`^`","(","x",",",2,")",")",",",1,")") # Here I construct the string, it could be vector as well?
> 
> S=noquote(paste(V,collapse=""))
> 
> S
[1] `-`(`+`(`^`(x,3),`^`(x,2)),1) # Here is the same as a character string.

Now I'd like to create a function using this string, something like this, but of course, this doesn't work:

S=as.expression(S)

F1<-function(x){S}

Is there some way to do this?

Cheers,

Atte Tenkanen
University of Turku, Finland



More information about the R-help mailing list