# Simple question on eval

5 messages
Open this post in threaded view
|

## Simple question on eval

 Hi group, I did some searches about this very simple question.  Hope someone can help me out. If I have the following: a <- "x <- 2^2" a [1] "x <- 2^2" How do I evaluate the expression that gets me an answer of 4?  I tried the following: > eval(a) [1] "x <- 2^2" > get(a) Error in get(a) : object 'x <- 2^2' not found (apparently it's not right!!) I have one long string of commands separated by semi-colon and hope to sort it out.  Thank you so much!!! Kel
Open this post in threaded view
|

## Re: Simple question on eval

 Hi Kel, Try this: eval(parse(text = a)) x Many times (though certainly not all), it may be easier/cleaner to rethink what you are doing (the step before you get a <- "x <- 2^2") to see if there is a simpler way. Cheers, Josh On Tue, Nov 30, 2010 at 2:24 PM, Lamke <[hidden email]> wrote: > > Hi group, > > I did some searches about this very simple question.  Hope someone can help > me out. > > If I have the following: > > a <- "x <- 2^2" > a > [1] "x <- 2^2" > > How do I evaluate the expression that gets me an answer of 4?  I tried the > following: > >> eval(a) > [1] "x <- 2^2" >> get(a) > Error in get(a) : object 'x <- 2^2' not found (apparently it's not right!!) > > I have one long string of commands separated by semi-colon and hope to sort > it out.  Thank you so much!!! > > Kel > -- > View this message in context: http://r.789695.n4.nabble.com/Simple-question-on-eval-tp3066346p3066346.html> Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > [hidden email] mailing list > https://stat.ethz.ch/mailman/listinfo/r-help> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code. > -- Joshua Wiley Ph.D. Student, Health Psychology University of California, Los Angeles http://www.joshuawiley.com/______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: Simple question on eval

 Thank you so much Joshua.  That's exactly what I am looking for. What I wanted to do is to pass a parameter to a function and I have to run the functions 30 times.  Instead of typing them all out, I created a long string of "f(a);f(b);f(c) ..." using paste() and use eval and parse to evaluative them all at once.  I am sure there are better ways of doing it but I just know this.