Thank you very much! I'm using the first example. For others: there are two simple typos in Henrik's email below: pretty sure y <- this$.; should be y <- this$.y;
Thanks again... Corrected below... Ben > Hello, > > I'd like to 'do work' on data members upon construction (i.e. without > implementing it in a get method). Is this the best way to create data member > 'z' upon construction? I'm thinking if .z=paste(x,y) below gets more complex > I'll run into issues. > > setConstructorS3("MyClass", function(x=NA,y=NA,...) { > this <- extend(Object(), "MyClass", > .x=x, > .y=y, > .z=paste(x,y) > ) Looks good to me and is standard R procedure where you work with the *arguments*. You can also work on the object after it's been instantiated, e.g. setConstructorS3("MyClass", function(x=NA,y=NA,...) { this <- extend(Object(), "MyClass", .x=x, .y=y, .z=NULL ) # Assign z this$.z <- paste(this$.x, this$.y); this; }) Note that if .z always a function of .x and .y it is redundant. Then an alternative is to create it on the fly, i.e. setMethodS3("getZ", "MyClass", function(this, ...) { x <- this$.x; y <- this$.y; z <- paste(x, y); z; }) You can then make it clever and cache the results so that it is only calculated once, e.g. setMethodS3("getZ", "MyClass", function(this, ...) { z <- this$.z; if (is.null(z)) { x <- this$.x; y <- this$.y; z <- paste(x, y); this$.z <- z; } z; }) However, you then have to make sure to reset z (this$.z <- NULL) whenever .x or .y is changed. /Henrik On Thu, Sep 22, 2011 at 10:18 AM, Henrik Bengtsson <h...@biostat.ucsf.edu>wrote: > On Thu, Sep 22, 2011 at 9:06 AM, Ben qant <ccqu...@gmail.com> wrote: > > Hello, > > > > I'd like to 'do work' on data members upon construction (i.e. without > > implementing it in a get method). Is this the best way to create data > member > > 'z' upon construction? I'm thinking if .z=paste(x,y) below gets more > complex > > I'll run into issues. > > > > setConstructorS3("MyClass", function(x=NA,y=NA,...) { > > this <- extend(Object(), "MyClass", > > .x=x, > > .y=y, > > .z=paste(x,y) > > ) > > Looks good to me and is standard R procedure where you work with the > *arguments*. You can also work on the object after it's been > instantiated, e.g. > > setConstructorS3("MyClass", function(x=NA,y=NA,...) { > this <- extend(Object(), "MyClass", > .x=x, > .y=y, > .z=NULL > ) > > # Assign z > this$.z <- paste(this$.x, this$.y); > > this; > }) > > Note that if .z always a function of .x and .y it is redundant. Then > an alternative is to create it on the fly, i.e. > > setMethodS3("getZ", "MyClass", function(this, ...) { > x <- this$.x; > y <- this$.; > z <- paste(x, y); > z; > }) > > You can then make it clever and cache the results so that it is only > calculated once, e.g. > > setMethodS3("getZ", "MyClass", function(this, ...) { > z <- this$.z; > if (is.null(z)) { > x <- this$.x; > y <- this$.; > z <- paste(x, y); > this$.z <- z; > } > z; > }) > > However, you then have to make sure to reset z (this$.z <- NULL) > whenever .x or .y is changed. > > /Henrik > > > > > }) > > setMethodS3("getX", "MyClass", function(this, ...) { > > this$.x; > > }) > > setMethodS3("getY", "MyClass", function(this, ...) { > > this$.y; > > }) > > setMethodS3("getZ", "MyClass", function(this, ...) { > > this$.z; > > }) > > > >> mc = MyClass('a','b') > >> mc$x > > [1] "a" > >> mc$y > > [1] "b" > >> mc$z > > [1] "a b" > > > > > > Thanks, > > > > ben > > > > [[alternative HTML version deleted]] > > > > ______________________________________________ > > R-help@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. > > > [[alternative HTML version deleted]] ______________________________________________ R-help@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.