Skip to content

eval in parent.frame or sys.frame(0)

3 messages · Stéphane Dray, Peter Dalgaard

#
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).

 > f1()
[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
 > f2()
[1] "----F2----"
[1] "a2 = global"
[1] "b2 = global"



Any help would be appreciated, Thanks.


 > sessionInfo()
R version 2.15.1 (2012-06-22)
Platform: x86_64-unknown-linux-gnu (64-bit)

locale:
  [1] LC_CTYPE=fr_FR.UTF-8       LC_NUMERIC=C
  [3] LC_TIME=fr_FR.UTF-8        LC_COLLATE=fr_FR.UTF-8
  [5] LC_MONETARY=fr_FR.UTF-8    LC_MESSAGES=C
  [7] LC_PAPER=C                 LC_NAME=C
  [9] LC_ADDRESS=C               LC_TELEPHONE=C
[11] LC_MEASUREMENT=fr_FR.UTF-8 LC_IDENTIFICATION=C

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods base

other attached packages:
[1] ade4_1.5-0

loaded via a namespace (and not attached):
[1] tools_2.15.1
#
On Jul 27, 2012, at 11:23 , St?phane Dray wrote:

            
Try quote(xy) in a number of places....
#
Thanks Peter. Sorry for this question... I really need holidays.

Cheers.
On 27/07/2012 13:30, peter dalgaard wrote: