[R] Transforming a string into a command

Jim Lemon jim at bitwrit.com.au
Tue Nov 29 11:14:28 CET 2011


On 11/29/2011 06:30 PM, Xu Wang wrote:
> David,
>
> Did my reply get orphaned or are you trying to help me realize that asking
> why something does not work is not a straightforward question? I'll try to
> cover both bases.
>
> I'll focus just on the first case that I don't understand. Suppose we have
> s<- "ln(a+b)"
> a<-1
> b<-2
> eval(parse(text=s))
> Error in eval(expr, envir, enclos) : could not find function "ln"
>
> Perhaps it's because I don't understand eval well (any good references for
> reading up on eval, parse, substitute, etc.?). But I expected it to produce
> the same as the following line:
> eval(parse(text="ln(a+b)"))
>
Hi Xu,

Try:

s<-"log(a+b)"
a<-1
b<-2
eval(parse(text=s))
[1] 1.098612

There is no "ln" function in the base package.

Jim



More information about the R-help mailing list