Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

in R, what is the difference between get("abc") and eval(parse(text="abc"))

Tags:

parsing

r

eval

I'd like to know the difference(s) between those two operations; in this example, they seem to work the same, but there must be situations where one will work and the other not. I just can't figure out when this would be the case.

> a <- 298
> eval(parse(text = "a"))
[1] 298
> get("a")
[1] 298

Thanks in advance

like image 416
Jason V Avatar asked Oct 16 '25 22:10

Jason V


1 Answers

in your example the two functions works the same. But the difference shows up when you want to evaluate an expression of a particular form, example :

eval(parse(text = "x <- 1"))

The eval(parse(...)) command will evaluate the expression in input while get will just return the value stocked in it, in this particular example it will not work with get, because it is an expression (not a variable). So eval(parse(...)) is primarily used to evaluate expressions that you construct (by using paste, etc) in your program.

like image 171
Mamoun Benghezal Avatar answered Oct 18 '25 12:10

Mamoun Benghezal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!