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
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 >
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
>
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
On Thu, 18 Nov 2004 12:05:34 +0100, Eric Lecoutre <lecoutre at stat.ucl.ac.be> 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.To work with lexical scoping, you could do it as myObject <- function(){ a <- 1 list( a=function() a, "a<-" = function(x, value) a <<- value, foo=function(b) { cat("b:",b,"\n") cat("a:", a,"\n") } ) } Then you can access the a property pretty easily, but changing it is really ugly:> m <- myObject() > m$a()[1] 1> m$foo()Error in cat("b:", b, "\n") : Argument "b" is missing, with no default> m$foo(1)b: 1 a: 1> (m$"a<-")(m, 4) > m$foo(1)b: 1 a: 4 It would be slightly nicer (but still ugly) if this worked:> (m$a)(m) <- 4Error: invalid function in complex assignment but it doesn't. Duncan Murdoch
Eric Lecoutre <lecoutre <at> stat.ucl.ac.be> writes:
:
: 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
An example of lexical scoping lose to yours
is found by issuing the R command:
demo(scoping)
However, you probably want to use the builtin
facilities, namely the S3 or S4 object oriented
system or possibly the more conventional oo system
defined in the R.oo package of the R.classes bundle
found at:
http://www.maths.lth.se/help/R/R.classes/
In S3 your problem is done like this:
# use S3 to define 2 objects of class "myObject" and a method
tmp <- list(a=1); class(tmp) <- "myObject"
tmp2 <- list(a=2); class(tmp2) <- "myObject"
foo <- function(obj, b) UseMethod("foo")
foo.myObject <- function(obj, b) cat("b", b, "\a",
obj$a, "\n")
# try out the method
foo(tmp, 3)
foo(tmp2, 5)