Skip to content

Transforming a string into a command

11 messages · Victor, Michael Kao, andrija djurovic +4 more

#
I would like to make a string executable, e.g,

s<- "ln(a+b)"
a<-1
b<-2

???? execute string s to obtain ln(a+b) ????

How can I make it?

Ciao fron Rome
Vittorio
#
This is one way to do it.

a = 1
b = 2
c = parse(text = "log(a + b)")
eval(c)

Hope this helps.

Cheers,
On 27/11/2011 11:16 a.m., Victor wrote:
1 day later
#
On Nov 29, 2011, at 2:00 AM, Xu Wang wrote:

            
Can you think of anything else we might need to know in order to  
answer that question?
#
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)"))

Xu


David Winsemius wrote
--
View this message in context: http://r.789695.n4.nabble.com/Transforming-a-string-into-a-command-tp4112183p4118294.html
Sent from the R help mailing list archive at Nabble.com.
#
Hm, try to set an object s before calling.

Regards
Petr
http://www.R-project.org/posting-guide.html
#
Hi
asking
to
have
What is function ln supposed to do and to what package it belongs. I get
No documentation for ?ln? in specified packages and libraries:
you could try ???ln?

but it does not mean that ln is not used elsewhere.

Regards
Petr
for
produce
http://r.789695.n4.nabble.com/Transforming-
http://www.R-project.org/posting-guide.html
#
On 11/29/2011 06:30 PM, Xu Wang wrote:
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
#
On Nov 29, 2011, at 2:30 AM, Xu Wang wrote:

            
All replies are "orphaned".  You are asked to include context if your  
question relies on code that has previously been posted.
It appears you need to review the help page for the `log` function.
It did.

 > eval(parse(text="ln(a+b)"))
Error in eval(expr, envir, enclos) : could not find function "ln"
They both "worked" as expected. An error was appropriately reported.

 > ln(a+b)
Error: could not find function "ln"

 > log(a+b)
[1] 1.098612
Some Nabble users seem to expect that the rest of Rhelp sees what they  
see. They are delusional when they do so.