[R] Lexical Scoping: eval(expr,envir=)

Dimitris Rizopoulos
Thu, 18 Nov 2004 12:51:59 -0000

Hi Eric,

this my novice point of view (since I'm still learning R) about what
is happening:

First, `tmp' is not an evironment. Check:

is.environment(tmp)
[1] FALSE

If you'd like to create an environment based on tmp then a simple way
could be:

e1 <- new.env()
for(i in seq(along=tmp)) assign(names(tmp)[[i]], tmp[[i]], envir=e1)

Then, `tmp$foo()' is defined in the enviroment of `myObject()', thus
you could set its evironment to be "e1" using:

environment(tmp$foo) <- e1

now

eval(tmp$foo(2), envir=e1)

maybe is what you want.

Of course maybe someone more experienced than me has a better
solution-explanation but I hope this helps.

Best,
Dimitrs

----
Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven

Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/16/396887
Fax: +32/16/337015
Web: http://www.med.kuleuven.ac.be/biostat
     http://www.student.kuleuven.ac.be/~m0390867/dimitris.htm


----- Original Message -----
From: "Eric Lecoutre" <lecoutre at stat.ucl.ac.be>
To: <r-help at stat.math.ethz.ch>
Sent: Thursday, November 18, 2004 12:05 PM
Subject: [R] Lexical Scoping: eval(expr,envir=)


>
> Hi R-listers,
>
> I am trying to better undertand what we would call "functional
> paradigm" use of S/R to better map my programming activities in
> other languages.
>
> This little function is aimed to create an object (at the end end,
> it would have it's own class):
>
> --
>   myObject =function(){
>     list(
>       a=1,
>       foo=function(b)
>       {
>       cat("b:",b)
>       cat("\na:", a)
>       }
>     )
>   }
> --
> To my minds, "a" would be a property of the object and
"foo" one of
> it's method.
>
> Let instantiate one version of this object:
>
> --
> > tmp = myObject()
> > tmp
> $a
> [1] 1
>
> $foo
> function(b)
>       {
>       cat("b:",b)
>       cat("\na:", a)
>       }
> <environment: 012DDFC8>
> --
>
> Now I try to "invoke it's foo method" (definitively not a S
> terminology!)
> For sure, tmp$foo() wont work, as it can't know anything about
"a".
>
> Reading eval() help page, It is said:
>
> envir: the 'environment' in which 'expr' is to be
evaluated.  May
>           also be a list, a data frame, or an integer as in
> 'sys.call' was
>
> so that I was thinking that
>
> > eval(tmp$foo(),envir=tmp)
> Error in cat("b:", b) : Argument "b" is missing, with
no default
>
> would solve my problem, which is not the case.
> tmp is a list, in which "a" is defined hand has a value.
>
> Where is my fault?
>
>
>
> Eric
>
> R version 2.0.1, Windows
>
>
>
>
> Eric Lecoutre
> UCL /  Institut de Statistique
> Voie du Roman Pays, 20
> 1348 Louvain-la-Neuve
> Belgium
>
> tel: (+32)(0)10473050
> lecoutre at stat.ucl.ac.be
> http://www.stat.ucl.ac.be/ISpersonnel/lecoutre
>
> If the statistics are boring, then you've got the wrong
> numbers. -Edward Tufte
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
>