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

francoisromain@free.fr francoisromain at free.fr
Thu Nov 18 13:35:15 CET 2004


Hello,

I'm quite new to the objet-oriented vision of life in R, but I think you are
looking for

?setClass

or other functions in the methods package.



Selon Eric Lecoutre <lecoutre at stat.ucl.ac.be>:

>
> 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