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 -- 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/
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.... -- Peter Dalgaard, Professor, Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederiksberg, Denmark Phone: (+45)38153501 Email: pd.mes at cbs.dk Priv: PDalgd at gmail.com
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/