[R] Inheriting from factors + co.

Torsten Steuernagel Torsten.Steuernagel at gmx.de
Wed Feb 25 11:43:34 CET 2004


On 25 Feb 2004 at 10:00, Uwe Ligges wrote:

> So you are going to handle/mix the S4 class like/with S3 classes? Hmm.

Yes, and there is the problem. With S4 classes, it works. Now I would 
expect that is(factor(S3object, "ANY") will be FALSE for factor and any 
other S3 class, but is(S3object, "ANY") always gives TRUE.

> It can be either of class "myclass" or of class "factor". I'd define a
> separate explicit slot for such a class:

It is of class "myclass" but it should inherit from "factor" by the .Data 
part if I specify a factor. That's what it does with S4 classes:

my1 <- new("myclass", 1:10)
class(my1)			#  "myclass"
is.numeric(my1)		# TRUE

my2 <- new("myclass", "abc")
class(my2)			#  "myclass"
is.character(my2)		# TRUE

In those cases the object actually is a numeric or a character and thus 
can be treated accordingly.
 
>    setClass("myclass", representation(mainSlot = "ANY",
>        x = "numeric", y = "numeric"))

That works, of course. But that's not what I'm looking for. This requires 
using the slot explicitly (not impossible, but I'd really prefer to have it in 
the .Data part). One main drawback is that R just doesn't accept S3 
classes as .Data part:

setClass("myclass", representation("factor"))

It creates a new class (although a warning is issued) and also extends 
from "factor" but doesn't create the .Data slot. That's also the reason 
why my second approach with setClassUnion() doesn't work as soon as 
an S3 class is part of the union. I just don't understand why S3 classes 
in explicit slots work perfectly well whereas it seems impossible to have 
them in the .Data slot. 

Torsten




More information about the R-help mailing list