[R] Simple question on eval

Joshua Wiley jwiley.psych at gmail.com
Tue Nov 30 23:38:47 CET 2010


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 <lamkelf at gmail.com> 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.
>
> ______________________________________________
> R-help at r-project.org 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/



More information about the R-help mailing list