[R] How to change the value of a class slot

Ross Boylan ross at biostat.ucsf.edu
Thu Jun 9 20:56:27 CEST 2005


On Wed, 2005-06-08 at 14:15 +0200, Thomas Petzoldt wrote:
...
> Hello Ross,
> 
> I see that your question was related to S4, but I just noticed a
> solution based on the R.oo package so I thought I would add a solution
> based on the proto package too. We had similar problems several times
> ago and (to my surprise) found R to be an extremely flexible language
> even for these things. Our favorite solution is available as
> package(proto). It requires R 2.1, because of several subtle
> improvements regarding environments, which made our implementation more
> streamlined.
> 
> Does the following example do what you intended?

Certainly looks like it.  It seems R has an embarrassment of object
systems.  R.oo, which someone else mentioned, also has the semantics I
was thinking of.  There's an interesting paper here
http://www.ci.tuwien.ac.at/Conferences/DSC-2003/Proceedings/Bengtsson.pdf
which contrasts what I was looking for, which it calls
class-object-oriented programming (COOP) with what R does, which it
calls functional-object-oriented programming (FOOP).

The paper also notes that call-by-value vs call-by-reference, which is
the root of the slot update problem, is really orthogonal to the
FOOP/COOP distinction.  It's easy to imagine FOOP with
call-by-reference.  R.oo uses references.

That paper also mentions that Omegahat has on OOP package, which
apparently is similar in spirit to R.oo while taking a more low-level
approach to the implementation.

Ross

> 
> ##=====================================================
> library(proto)
> 
> ## 1) define an object
> CompletePathMaker <- proto(
>       index = 0,
>       bumpIndex = function(., dindex = 1)
>         .$index <- .$index + as.integer(dindex)
> )
> 
> ## 2) create a child object of CompletePathMaker
> cpm <- CompletePathMaker$proto()
> 
> ## 3) set the index component to 3
> cpm$index <- 3
> 
> ## 4) iterate the index
> cpm$bumpIndex(2)
> 
> ## print the result
> cpm$index
> 
> ##=====================================================




More information about the R-help mailing list