Re: [R] help with oop in R - class structure and syntex

From: tom soyer <tom.soyer_at_gmail.com>
Date: Wed, 6 Feb 2008 10:13:30 -0600

Thanks Gabor. I guess true oo encapsulation is not possible in R.

It seems that there is an IDE for S+ in Eclipse...

On 2/6/08, Gabor Grothendieck <ggrothendieck_at_gmail.com> wrote:
>
> On Feb 6, 2008 9:45 AM, tom soyer <tom.soyer@gmail.com> wrote:
> > Thanks Gabor for illustrating the basics oop in R using S3. Maybe I
> didn't
> > have the right documents, but you example taught me more about oop in R
> than
> > everything else I read combined! Thanks for the tip on R.oo, I plan to
> check
> > it out later.
> >
> > I have a few followup questions...
> >
> > (1) how do I encapsulate the generics? i.e., if a class has 100 methods,
> > then does it mean 100 generics would be dumped in the global
> environment?
> > Or, is it possible to define a local environment and restrict the
> generics
> > from one class to a particular environment? But then how do you call the
> > generics from a special environment? Also, is it possible to inherit
> classes
> > across different environment?
>
> Both ofthe following return 3. If that is not what you are asking
> then I suggest you experiment with a few small examples of
> this nature:
>
> f <- function() {
> F <- function(x) UseMethod("F")
> F.default <- function(x) x
> F(3)
> }
> F.default <- function(x) NULL
> f()
>
>
> F <- function(x) UseMethod("F")
> f <- function() {
> F.default <- function(x) x
> F(3)
> }
> F.default <- function(x) NULL
> f()
>
> >
> > (2) it seems that an R specific IDE would improve productivity
> dramatically
> > (maybe even necessary?) with respect to oop. Is there such an IDE and
> does
> > it work for oop? I recall a group (in Germany?) was working on it but I
> > can't remember where I read about it.
> >
> > Thanks!
> >
> >
> > On 2/5/08, Gabor Grothendieck <ggrothendieck_at_gmail.com> wrote:
> > > This illustration uses S3. Note that functions do not modify their
> > arguments
> > > so to modify an object we have to pass it to the method and then pass
> the
> > > object back. There is also another system called S4 which involves
> typing
> > > of arguments and there are packages proto and R.oo which provide
> different
> > > OO models.
> > >
> > >
> > > # define constructors for bicycle and mountainBike classes
> > > Bicycle <- function(cadence, gear, speed) structure(list(cadence =
> > > cadence, gear = gear, speed = speed), class = "bicycle")
> > > MountainBike <- function(cadence, gear, speed, seatHeight) {
> > > x <- Bicycle(cadence, gear, speed)
> > > x$seatHeight <- seatHeight
> > > class(x) <- c(class(x), "mountainBike")
> > > x
> > > }
> > >
> > > # define generic setCadence and then methods for each class
> > >
> > > setCadence <- function(x, cadence) UseMethod("setCadence")
> > > setCadence.bicycle <- function(x, cadence) { x$cadence <- cadence; x }
> > >
> > > # mountBike's setCadence method overrides bicycle's setCadence method
> > > setCadence.mountBike <- function(x, cadence) { x$cadence <- cadence +
> 1; x
> > }
> > >
> > > # list the setCadence methods avialable
> > > methods(setCadence)
> > >
> > > # define a generic applyBrake and a "bicycle" method
> > > # mountainBike will inherit the bicycle method by default
> > > applyBrake <- function(x, decrement) UseMethod("applyBrake")
> > > applyBrake.bicycle <- function(x, decrement) { x$speed <- x$speed -
> > decrement }
> > >
> > > # list the applyBrake methods available
> > > methods(applyBrake)
> > >
> > > b <- Bicycle(1, 2, 3)
> > > m <- MountainBike(4, 5, 6, 7)
> > > m <- applyBrake(m, 1)
> > >
> > >
> > > On Feb 5, 2008 8:21 AM, tom soyer <tom.soyer_at_gmail.com> wrote:
> > > > Hi,
> > > >
> > > > I read section 5, oop, of the R lang doc, and I am still not sure I
> > > > understand how to build a class in R for oop. I thought that since I
> > > > understand the oop syntex of Java and VB, I am wondering if the R
> > programmig
> > > > experts could help me out by comparing and contrasting the oop
> syntex in
> > R
> > > > with that of Java. For example, the basic class structure in Java is
> > like
> > > > this:
> > > >
> > > > public class Bicycle {
> > > >
> > > > // *the Bicycle class has three fields*
> > > > public int cadence;
> > > > public int gear;
> > > > public int speed;
> > > >
> > > > // *the Bicycle class has one constructor*
> > > > public Bicycle(int startCadence, int startSpeed, int startGear) {
> > > > gear = startGear;
> > > > cadence = startCadence;
> > > > speed = startSpeed;
> > > > }
> > > >
> > > > // *the Bicycle class has four methods*
> > > > public void setCadence(int newValue) {
> > > > cadence = newValue;
> > > > }
> > > >
> > > > public void setGear(int newValue) {
> > > > gear = newValue;
> > > > }
> > > >
> > > > public void applyBrake(int decrement) {
> > > > speed -= decrement;
> > > > }
> > > >
> > > > public void speedUp(int increment) {
> > > > speed += increment;
> > > > }
> > > >
> > > > }
> > > >
> > > > Could one of the R experts please illustrate the R class syntex for
> > writing
> > > > the R equivalent of the Java Bicycle class above?
> > > >
> > > > Also, in Java, inheritance is done like this:
> > > >
> > > > public class MountainBike extends Bicycle {
> > > >
> > > > // *the MountainBike subclass has one field*
> > > > public int seatHeight;
> > > >
> > > > // *the MountainBike subclass has one constructor*
> > > > public MountainBike(int startHeight, int startCadence, int
> > startSpeed,
> > > > int startGear) {
> > > > super(startCadence, startSpeed, startGear);
> > > > seatHeight = startHeight;
> > > > }
> > > >
> > > > // *the MountainBike subclass has one method*
> > > > public void setHeight(int newValue) {
> > > > seatHeight = newValue;
> > > > }
> > > >
> > > > }
> > > >
> > > > What would be the R oop syntex for inheritance in the case of the
> > > > MontainBike class?
> > > >
> > > > Thanks!
> > > >
> > > > --
> > > > Tom
> > > >
> > > > [[alternative HTML version deleted]]
> > > >
> > > > ______________________________________________
> > > > R-help_at_r-project.org mailing list
> > > > https://stat.ethz.ch/mailman/listinfo/r-help
> > > > PLEASE do read the posting guide
> > http://www.R-project.org/posting-guide.html
> > > > and provide commented, minimal, self-contained, reproducible code.
> > > >
> > >
> >
> >
> >
> > --
> > Tom
>

-- 
Tom

	[[alternative HTML version deleted]]

______________________________________________
R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Received on Wed 06 Feb 2008 - 16:17:13 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 06 Feb 2008 - 17:30:12 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive