Hello. Suppose you define a new S4-class, say> setClass("track", representation(x="numeric", y="numeric"))Don't worry if you have a deja vu, it's from the help page. Your new class is said to have a fixed structure: two slots, x, and y, and that should apply to all objects you construct as members of that class.> tr <- new( "track" )Now do the following:> tr[ "ping" ] <- "pong" > tr$bingo <- "bongo" > tr[[ "blaa" ]] <- c( 200, 300 )Of course you can use the well known operators to access these "list entries in a S4-class object":> tr[ "blaa" ] > etc.You see what can happen if you decide to do a bit stress testing. The question is not whether my examples makes sense or not. The question is if these examples should be possible at all. I wonder which is true, -- the theory (Chambers, 1998, p. 279ff; Venables and Ripley, 2000, p. 99ff: "... All objects in a [S4] class must have the same structure. ..."; etc.) -- or the actual implementation in R (see my example, which successfully violates the design principles of the language) Bug, or feature? Any clarifications are appreciated -- it may be my half-cooked knowledge that I find this confusing and dangerous. Best Joerg Beyer P.S.: Oh, the specs: PowerMac G4/400 PCI -- 1GB RAM -- Mac OS X 10.4.6 -- R 2.2.1
Gabor Grothendieck
2006-Aug-28 14:08 UTC
[Rd] S4 classes and objects -- fixed structure? No...
Under R 2.3.1 these work as you indicate but under R 2.4.0 they all give errors:> setClass("track", representation(x="numeric", y="numeric"))[1] "track"> tr <- new( "track" ) > tr[ "ping" ] <- "pong"Error in "[<-"(`*tmp*`, "ping", value = "pong") : object is not subsettable> tr$bingo <- "bongo"Error: cannot coerce type S4 to list vector In addition: Warning message: Coercing LHS to a list> tr[[ "blaa" ]] <- c( 200, 300 )Error in "[[<-"(`*tmp*`, "blaa", value = c(200, 300)) : object is not subsettable> tr[ "blaa" ]Error in tr["blaa"] : object is not subsettable> R.version.string[1] "R version 2.4.0 Under development (unstable) (2006-08-08 r38825)" On 8/28/06, J?rg Beyer <Beyerj at students.uni-marburg.de> wrote:> Hello. > > Suppose you define a new S4-class, say > > setClass("track", representation(x="numeric", y="numeric")) > > Don't worry if you have a deja vu, it's from the help page. > Your new class is said to have a fixed structure: two slots, x, and y, > and that should apply to all objects you construct as members of that class. > > tr <- new( "track" ) > > Now do the following: > > tr[ "ping" ] <- "pong" > > tr$bingo <- "bongo" > > tr[[ "blaa" ]] <- c( 200, 300 ) > > Of course you can use the well known operators to access these "list entries > in a S4-class object": > > tr[ "blaa" ] > > etc. > > You see what can happen if you decide to do a bit stress testing. The > question is not whether my examples makes sense or not. The question is if > these examples should be possible at all. > I wonder which is true, > -- the theory (Chambers, 1998, p. 279ff; Venables and Ripley, 2000, p. 99ff: > "... All objects in a [S4] class must have the same structure. ..."; etc.) > -- or the actual implementation in R (see my example, which successfully > violates the design principles of the language) > > Bug, or feature? Any clarifications are appreciated -- it may be my > half-cooked knowledge that I find this confusing and dangerous. > > Best > > Joerg Beyer > > P.S.: > Oh, the specs: PowerMac G4/400 PCI -- 1GB RAM -- Mac OS X 10.4.6 -- R 2.2.1 > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel >
I think you're right---this shouldn't happen in theory, but it does because of the internal representation of S4 objects in R. In R devel (to be 2.4.0), this changes and I believe your example will no longer work. -roger J?rg Beyer wrote:> Hello. > > Suppose you define a new S4-class, say >> setClass("track", representation(x="numeric", y="numeric")) > > Don't worry if you have a deja vu, it's from the help page. > Your new class is said to have a fixed structure: two slots, x, and y, > and that should apply to all objects you construct as members of that class. >> tr <- new( "track" ) > > Now do the following: >> tr[ "ping" ] <- "pong" >> tr$bingo <- "bongo" >> tr[[ "blaa" ]] <- c( 200, 300 ) > > Of course you can use the well known operators to access these "list entries > in a S4-class object": >> tr[ "blaa" ] >> etc. > > You see what can happen if you decide to do a bit stress testing. The > question is not whether my examples makes sense or not. The question is if > these examples should be possible at all. > I wonder which is true, > -- the theory (Chambers, 1998, p. 279ff; Venables and Ripley, 2000, p. 99ff: > "... All objects in a [S4] class must have the same structure. ..."; etc.) > -- or the actual implementation in R (see my example, which successfully > violates the design principles of the language) > > Bug, or feature? Any clarifications are appreciated -- it may be my > half-cooked knowledge that I find this confusing and dangerous. > > Best > > Joerg Beyer > > P.S.: > Oh, the specs: PowerMac G4/400 PCI -- 1GB RAM -- Mac OS X 10.4.6 -- R 2.2.1 > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel >-- Roger D. Peng | http://www.biostat.jhsph.edu/~rpeng/