[R] inheritence in S4

cgenolin at u-paris10.fr cgenolin at u-paris10.fr
Tue Mar 4 11:31:39 CET 2008


Hi Martin, thanks for your answer

> But a couple
> of other quick points. I would have written
>
> setMethod("initialize", "A",
>          function(.Object, ..., xValue=numeric(0)){
>              callNextMethod(.Object, ..., x=xValue)
>          })
>

I am not that much familiar with S3... In our way of writing this 
method, 'initialize' for 'A' will call the next method for A, which is 
'initialise' for 'numeric', is that right ?

> And finally, the position of 'xValue' and 'yValue' means that
> the arugment has to be named, e.g., new("B", yValue=12). This seems a
> little awkward at first, but seems like a best practice

I agree with you. But I do not like the use of ... , it lets us to make 
many mistake like in :

print(3.5165,digitts=2)

There is a typo in digitts, but since print works with ... , R does not 
detect the mistake.
So I agree with the importance of naming argument, I always do it but 
without 'forcing' it

> And finally, in Jim's thread I mention using a constructor. So in
> practice for a case like the above I would not define any initialize
> methods,

Interesting. Will you still define a validity method or not even ?


Christophe


>
> Christophe Genolini <cgenolin at u-paris10.fr> writes:
>
>> 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.
>>>>
>>>
>>>
>>
>
> --
> Martin Morgan
> Computational Biology / Fred Hutchinson Cancer Research Center
> 1100 Fairview Ave. N.
> PO Box 19024 Seattle, WA 98109
>
> Location: Arnold Building M2 B169
> Phone: (206) 667-2793
>



----------------------------------------------------------------
Ce message a ete envoye par IMP, grace a l'Universite Paris 10 Nanterre



More information about the R-help mailing list