Simple question on eval

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Simple question on eval

Lamke
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
Reply | Threaded
Open this post in threaded view
|

Re: Simple question on eval

Joshua Wiley-2
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-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Simple question on eval

Lamke
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.
Reply | Threaded
Open this post in threaded view
|

Re: Simple question on eval

Ivan Calandra
Isn't it what do.call() does?

Ivan

Le 12/1/2010 16:39, Lamke a écrit :
> 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.

--
Ivan CALANDRA
PhD Student
University of Hamburg
Biozentrum Grindel und Zoologisches Museum
Abt. Säugetiere
Martin-Luther-King-Platz 3
D-20146 Hamburg, GERMANY
+49(0)40 42838 6231
[hidden email]

**********
http://www.for771.uni-bonn.de
http://webapp5.rrz.uni-hamburg.de/mammals/eng/1525_8_1.php

______________________________________________
[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.
Reply | Threaded
Open this post in threaded view
|

Re: Simple question on eval

Joshua Wiley-2
In reply to this post by Lamke
Here are two options that might work for you given the little bit you've said:

## If its the same parameter all 30 times
## say, for example, base = 4.5 to log
for(i in 1:30) {
  print(log(1:10, base = 4.5))
}

## if they are different parameters, you could try
lapply(X = c(1.3, 3, 2.2, 4, 5), FUN = function(x) {
  log(1:10, base = x)})

## the 'X' argument to lapply() is based, one at a time,
## to whatever the function is (the 'FUN' argument)
## so the above is equivalent to
log(1:10, base = 1.3)
log(1:10, base = 3)
log(1:10, base = 2.2)
log(1:10, base = 4)
log(1:10, base = 5)
## but easier to type

Cheers,

Josh

@Ivan do.call() passes a list of arguments, but I am not sure it would
easily adapt to passing 30 different arguments one at a time or even
the same argument 30 times.

On Wed, Dec 1, 2010 at 7:39 AM, Lamke <[hidden email]> wrote:

>
> 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.
> --
> View this message in context: http://r.789695.n4.nabble.com/Simple-question-on-eval-tp3066346p3067508.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-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.