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

Eric Lecoutre lecoutre at stat.ucl.ac.be
Thu Nov 18 14:49:36 CET 2004



Hi again,

In a sense, I have answered myself my question.
The functional paradigm is very well described in the article
"lexical scope and Statistical computing" by Ross Ihaka and Robert Gentleman.
And I did have read it several times...


Solution is function closure. And following code will work as I want.
Except i dont understand the help page on "eval".
What about the ability to pass a list for the value of environment()? Can I 
have an example of such a use?


My (now working) code:

---

 > ### First version: function closure
 >
 >   myObject1 =function(){
+     # Function Closure
+     function(){
+       a=1
+       list(foo=function(b=3)
+       {
+       cat("b:",b)
+       cat("\na:", a)
+       }
+        )
+       }
+   }
 >
 >   (tmp=myObject())
function(){
       a=1
       list(
       foo=function(b=3)    {
         cat("b:",b)
         cat("\na:", a)
         },
        set.a=function(newval) a <<-newval
        )
       }
<environment: 012B2CAC>
 >   tmp()$foo()
b: 3
a: 1>   tmp()$foo(b=32)
b: 32
a: 1>
 > ### Second version: add a function that allows to change the property a
 >     myObject2 =function(){
+     function(){
+       a=1
+       return(list(
+       foo=function(b=3)    {
+         cat("b:",b)
+         cat("\na:", a)
+         },
+        set.a=function(newval) a <<-newval
+        ))
+       }
+   }
 >
 >    (tmp=myObject2()())
$foo
function(b=3)    {
         cat("b:",b)
         cat("\na:", a)
         }
<environment: 012BBF08>

$set.a
function(newval) a <<-newval
<environment: 012BBF08>

 >   tmp$foo(b=32)
b: 32
a: 1>   tmp$set.a(10)
 >   tmp$foo(b=32)
b: 32
a: 10>

---

This achieves exactly the object-oriented aspect I wanted to have. And in 
fact myObject()() acts as a new instantiation of my object.

Best wishes,

Eric




At 12:05 18/11/2004, Eric Lecoutre wrote:

>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




More information about the R-help mailing list