[R] inheritence in S4

Christophe Genolini cgenolin at u-paris10.fr
Mon Mar 3 15:11:50 CET 2008


Thanks Martin

Well it works except that "as" seems to not like the "initialize" method 
: the following code (that is the same than yours with some initialize 
for A B and C) does not compile. It seems that as(c,"A") does not work 
if we definie a initialize for A...

--- 8< --------------
setClass("A", representation(x="numeric"))
setMethod("initialize","A",function(.Object,value){.Object at x <- 
value;return(.Object)})
a <- new("A",4)

setClass("B", representation(y="numeric"))
setMethod("initialize","B",function(.Object,value){.Object at y <- 
value;return(.Object)})
b <- new("B",5)

setClass("C", contains=c("A", "B"))
setMethod("initialize","C",function(.Object,valueA, valueB){
    .Object at x <- valueA
    .Object at y <- valueB
    return(.Object)
})
c <- new("C",valueA=10,valueB=12)

setMethod("show", "A", function(object) cat("A\n"))
setMethod("show", "B", function(object) cat("B\n"))
setMethod("show", "C", function(object) {
    callGeneric(as(object, "A"))
    callGeneric(as(object, "B"))
    cat("C\n")
})
c
--- 8< --------------------

Is there something wrong with the use of 'as' between class and father 
class?

Christophe
> Hi Christophe -- 
>
> I don't know whether there's a particularly elegant way. This works
>
> setClass("A", representation(x="numeric"))
> setClass("B", representation(y="numeric"))
> setClass("C", contains=c("A", "B"))
>
> setMethod("show", "A", function(object) cat("A\n"))
> setMethod("show", "B", function(object) cat("B\n"))
> setMethod("show", "C", function(object) {
>     callGeneric(as(object, "A"))
>     callGeneric(as(object, "B"))
>     cat("C\n")
> })
>
>   
>> new("C")
>>     
> A
> B
> C
>
> but obviously involves the developer in making explicit decisions
> about method dispatch when there is multiple inheritance.
>
> Martin
>
> cgenolin at u-paris10.fr writes:
>
>   
>> Hi the list
>>
>> I define a class A (slot a and b), a class C (slot c and d) and a class 
>> E that inherit from A and B.
>> I define print(A) and print(B). For print(C), I would like to use both 
>> of them, but I do not see how...
>>
>> Thanks for your help...
>>
>> Christophe
>>
>> ----------------------------------------------------------------
>> Ce message a ete envoye par IMP, grace a l'Universite Paris 10 Nanterre
>>
>> ______________________________________________
>> 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.
>>     
>
>



More information about the R-help mailing list