Re: [Rd] setIs and method dispatch in S4 classes

From: Seth Falcon <>
Date: Fri 07 Apr 2006 - 14:56:56 GMT

Hi Peter,

Peter Ruckdeschel <> writes:
> ## now: B00 mother class to B01 and B02, and again B02 "contains" B01 by
> setIs:
> setClass("B00", representation(a="numeric"))
> setClass("B01", representation(a="numeric",b="numeric"), contains= "B00")
> setClass("B02", representation(a="numeric",d="numeric"), contains= "B00")
> setIs("B02","B01",coerce=function(obj){new("B01", a=obj@a, b=obj@d)},
> replace=function(obj,value){new("B01", a=value@a, b=value@b)})
> # now two "+" methods for B00 and B01
> setMethod("+", signature=c("B00","B00"), function(e1,e2){e1@a+e2@a})
> setMethod("+", signature=c("B01","B01"), function(e1,e2){e1@b+e2@b})
> x1=new("B02", a=1, d=2)
> x2=new("B02", a=1, d=3)
> x1+x2 ## 2 --- why?

My impression from reading over the man page for setIs, is that it isn't intended to be used to override the existing inheritance hierarchy. It also mentions that the return value is the extension info as a list, so that could also be useful in understanding what setIs is doing. Here's the output for your example:


    Name:        a       d

    Class: numeric numeric     

    Class "B00", directly
    Class "B01", directly, with explicit coerce

Use the contains arg of setClass to define the superclasses. With the contains arg, the order determines the precedence for method lookup. But I suspect you know that already.

> Is there a possibility to force usage of the B01 method /without/
> explicitely coercing x1,x2 to B01, i.e. interfere in the dispatching
> precedence, telling R somehow (by particular arguments for setIs ?)
> to always use the is-relation defined by setIs first before mounting
> the hierarchy tree?

Perhaps explaining a bit more about what you are trying to accomplish will allow someone to provide a more helpful suggestion than mine :-)

If you know the inheritance structure you want before run-time, then I'm not seeing why you wouldn't just use the contains arg.

And if you want to force certain behavior at run-time, then I don't see what's wrong with an explicit coercion using as(foo, "bar").

But perhaps I'm missing something.


+ seth mailing list Received on Sat Apr 08 01:23:20 2006

This archive was generated by hypermail 2.1.8 : Fri 07 Apr 2006 - 20:17:00 GMT