On Jul 27, 2012, at 11:23 , St?phane Dray wrote:
Dear list,
I try to use the eval function and to understand its functionning. I tried:
xy <- "global"
f1 <- function(){
xy <- "f1"
a1 <- eval.parent(xy)
b1 <- eval(xy, sys.frame(0))
print(paste("a1 =",a1))
print(paste("b2 =",b1))
f3 <- function(){
print("----F3----")
a3 <- eval.parent(xy)
b3 <- eval(xy, sys.frame(0))
print(paste("a3 =",a3))
print(paste("b3 =",b3))
}
f2()
f3()
}
f2 <- function(){
print("----F2----")
a2 <- eval.parent(xy)
b2 <- eval(xy, sys.frame(0))
print(paste("a2 =",a2))
print(paste("b2 =",b2))
}
f1()
f2()
I obtain the following results (and add as comments what I would expect after reading the doc). I really do not understand why the evaluation in sys.frame(0) (i.e. global environment) did not work. Moreover, the evaluation through eval.parent should be linked to the environment of the caller and not the definition (so I do not understand why f2 and f3 produces different results).
[1] "a1 = f1" ## global ?
[1] "b2 = f1" ## global
[1] "----F2----"
[1] "a2 = global" ## f1
[1] "b2 = global" ## global
[1] "----F3----"
[1] "a3 = f1" ## f1
[1] "b3 = f1" ## global
[1] "----F2----"
[1] "a2 = global"
[1] "b2 = global"
Any help would be appreciated, Thanks.
Try quote(xy) in a number of places....