[Rd] S4 extends a class, but .Data slot has different class

John Chambers jmc at R-project.org
Mon Apr 18 22:11:47 CEST 2005


giles.heywood at uk.abnamro.com wrote:

> When I define an S4 class ("B" in the example below) that directly extends
> another ("A" in the example below) , which in turn directly extends another
> ("character" in the example below), I find that the slot does not have the
> class I specified in setClass(), it has the underlying class.
> 
> Is this an intended feature?

Well, it seems to be an implication of how classes work.

An object from an S4 class is implemented in R by slots (which are in 
fact attributes).  The .Data "slot", if there is one, is special that it 
is in fact the actual vector of data to which the real slots are 
attached.  This implementation makes many things back-compatible that 
would not be otherwise.

The .Data slot of class B is inherited in your example from the .Data 
slot of class A & therefore it has class "character" as illustrated.

writing

setClass("B",representation("A"))

does NOT say to make "A" the .Data slot of "B".  It says that "B" should 
inherit all the slots of "A".  The fact that extending a basic vector 
class DOES implicitly define the .Data slot is a somewhat special case.

In R, we would prefer for stylistic reasons to write the above as:

setClass("B", contains = "A")

This has the exact same effect, I think, but is perhaps clearer in 
saying that "B" should contain all the same slots as "A" (and therefore, 
should have a .Data slot of class "character").

> 
> Briefly, the reason for using this type of structure is to avoid the issue
> which I posted to this list before, where in 2.0.1 matrix no long 'is'
> array for the purposes of S4, and therefore an extract drop=TRUE can, in a
> special case, lead to an error.
> 
> Giles Heywood
> 
> - - - - -
> 
> Example:
> 
> 
>>setClass("A",representation("character"))
> 
> [1] "A"
> 
>>setClass("B",representation("A"))
> 
> [1] "B"
> 
>>getClass("B")
> 
> 
> Slots:
> 
> Name:      .Data
> Class: character
> 
> Extends:
> Class "A", directly
> Class "character", by class "A"
> Class "vector", by class "A"
> 
> 
>>is(new("B",new("A","abc"))@.Data,"A")
> 
> [1] FALSE
> 
> ---------------------------------------------------------------------------
> This message (including any attachments) is confidential and...{{dropped}}
> 
> ______________________________________________
> R-devel at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>



More information about the R-devel mailing list