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

Eric Lecoutre lecoutre at stat.ucl.ac.be
Thu Nov 18 12:05:34 CET 2004


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




More information about the R-help mailing list