[R] eval in parent.frame or sys.frame(0)

Stéphane Dray stephane.dray at univ-lyon1.fr
Fri Jul 27 13:44:56 CEST 2012


Thanks Peter. Sorry for this question... I really need holidays.

Cheers.

On 27/07/2012 13:30, peter dalgaard wrote:
> 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).
>>
>>> 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.
> Try quote(xy) in a number of places....
>


-- 
Stéphane DRAY (stephane.dray at univ-lyon1.fr)
Laboratoire BBE-CNRS-UMR-5558, Univ. C. Bernard - Lyon I
43, Bd du 11 Novembre 1918, 69622 Villeurbanne Cedex, France
Tel: 33 4 72 43 27 57       Fax: 33 4 72 43 13 88
http://pbil.univ-lyon1.fr/members/dray/



More information about the R-help mailing list