[Rd] How to add a slot to S4 class of an existing package?

cstrato cstrato at aon.at
Mon Dec 22 20:39:37 CET 2008


Dear all,

Since my package is based on S4 classes, I would like to know how to add 
a slot to an existing S4 class without breaking the code of users of my 
package.

Assume the following S4 class:
setClass("MyClass",
   representation(name = "character",
                  type = "character",
                  data = "data.frame"
   ),
   prototype(name = "",
             type = "Default",
             data = data.frame(matrix(nr=0,nc=0))
   )
)#MyClass

Assume that a user has created an object:
 > myclass <- new("MyClass", name="MyName", type="MyType", data=tmp)
 > str(myclass)


Now I would like to add another slot "info" to MyClass:
setClass("MyClass",
   representation(name = "character",
                  type = "character",
                  data = "data.frame",
                  info = "data.frame"
   ),
   prototype(name = "",
             type = "Default",
             data = data.frame(matrix(nr=0,nc=0)),
             info = data.frame(matrix(nr=0,nc=0))
   )
)#MyClass

Now when the user loads my package with S4 class MyClass containing a 
new slot and calls:
 > str(myclass)
Error in FUN(c("name", "type", "data", "info")[[4L]], ...) :
  no slot of name "info" for this object of class "MyClass"
 >

My question is:
Is there any possibility or special trick, which would avoid this error 
message?

Are there other possibilities to access an additional data.frame from an 
existing class?

Is there something like an "evolution" of S4 classes, which 
distinguishes the different implementations of an S4 class, and allows 
the user to keep the object of an old class?

Best regards
Christian
_._._._._._._._._._._._._._._._._._
C.h.r.i.s.t.i.a.n   S.t.r.a.t.o.w.a
V.i.e.n.n.a           A.u.s.t.r.i.a
e.m.a.i.l:        cstrato at aon.at
_._._._._._._._._._._._._._._._._._



More information about the R-devel mailing list