[R] Equation as a character string

Gabor Grothendieck ggrothendieck at gmail.com
Thu Mar 15 18:45:08 CET 2012


On Thu, Mar 15, 2012 at 12:58 PM, Rui Barradas <rui1174 at sapo.pt> wrote:
> Hello,
>
>>
>> Hi Emily,
>>
>> Yes (see below), but you might be better off by writing a simple
>> function.  Here are examples both ways (usually eval parse is highly
>> discouraged).
>>
>> Cheers,
>>
>> Josh
>>
>
> Yes, eval/parse is discouraged but there's a way of using it,
> that is less troublesome, to create a function.
>
> (Maybe Emily was thinking of symbolic mathematics software - my personal
> favorite is
> Maple but there is also open source Maxima)
>
>
> makefun <- function(text){
>        x <- numeric()
>        function(x) eval(parse(text=text))
> }
>
> txt <- 'x^2 + x + 5'
>
> g <- makefun(txt)

Another way to do this which uses parse but not eval is:

g2 <- function(x) {}
body(g2) <- parse(text = txt)


-- 
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com



More information about the R-help mailing list