[R] Extending a group of S4 classes by setClassUnion ?

Alexander juschitz_alexander at yahoo.de
Thu Mar 15 15:57:28 CET 2012


Hi,
I would like to create some S4 classes as follows

setClass("Father",representation(name="character"))
setClass("Son1",contains="Father",representation(par="numeric"))
setClass("Son2",contains="Father",representation(par="logical"))

Son1<-new("Son1")
Son1 at name<-"Son1"
Son1 at par<-3

Son2<-new("Son2")
Son2 at name<-"Son2"
Son2 at par<-TRUE

setGeneric("get.par",function(object){standardGeneric ("get.par")})
setMethod("get.par","Son1",function(object){return(object at par+3)})
setMethod("get.par","Son2",function(object){return(!object at par)})

get.par(Son1)
get.par(Son2)

So far, so good. I would like now, to create a new class, which
"extends"/"contains" the subclasses of Father by some additional slots.
Is there any clean and simple possibility to inherite also the corresponding
function get.par ?

setClass("Extension",representation(person="Father",text="character"))
Ext<-new("Extension")
Ext at text<-"new try"
Ext at person<-Son1
get.par(Ext)
get.par(Ext at person)

Of course, "get.par(Ext)" returns an error. Is there any possibility to tell
R, that if now function exists for a Class, to transform the object to a
class, for which the function exists ? (I know, it is not very clear what I
am writing, but I am doing my best). I don't want to rewrite every method
for  "Extension" like

setMethod("get.par","Extension",function(object){get.par(object at person)})

Is there any simpler solution by steClassUnion, setAs, setIs .... ?

Thanks a lot in advance

Alexander

--
View this message in context: http://r.789695.n4.nabble.com/Extending-a-group-of-S4-classes-by-setClassUnion-tp4475251p4475251.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list