[R] Fw: Having problem to define a subclass, please help me

Megh Dal megh700004 at yahoo.com
Fri Jul 30 20:21:39 CEST 2010


Dear all, apart from my previous question, I would also like to ask one more question here, which is as follows:

#let me 1st define a class and a subclass
setClass("a", representation=list(x="numeric", y="numeric"), prototype=list(x=rep(1,3),y=rep(2,3)))
setClass("b", representation=list(x1="character", y1="character"), prototype=list(x1=rep("A",3),y1=rep("B",3)), contains="a")

#Now I define the method for initialize() for class "a"
setMethod("initialize", "a", function(.Object, x, y, ...) {
                              if (length(x) != length(y)) x=y=rep(10,4)
                              .Object at x = x
                              .Object at y = y
                              .Object })
new("a", x=rnorm(4), y=rnorm(4))
new("a", x=rnorm(4), y=rnorm(3))

#Next I define method for initialize() for subclass "b"

setMethod("initialize", "b", function(.Object, x1, y1, ...) {
                              if (length(x1) == length(y1)) x1=y1=rep("bbb",4)
                              .Object at x1 = x1
                              .Object at y1 = y1
                              .Object })

> new("b", x1=letters[1:3], x2=letters[2:4], x=rnorm(4), y=rnorm(3))
An object of class "b"
Slot "x1":
[1] "bbb" "bbb" "bbb" "bbb"

Slot "y1":
[1] "bbb" "bbb" "bbb" "bbb"

Slot "x":
[1] 1 1 1

Slot "y":
[1] 2 2 2

> new("b", x1=letters[1:3], x2=letters[2:4], x=rnorm(4), y=rnorm(4))
Error in checkSlotAssignment(object, name, value) :
  assignment of an object of class "numeric" is not valid for slot "y1" in an object of class "b"; is(value, "character") is not TRUE

Here my questions are:
1. Why I am getting the prototype object in next to previous example for slots x & y?
2. Why just previous example generates some error?
3. The method initialize() function for a subclass requires explicit description of all slots of it's superclass? If yes why? In my understanding, all defined law for super-class should be inherited by it's sub-class, therefore no need to define again.

I would be really grateful if someone clarify those.

Thanks


--- On Fri, 7/30/10, Megh Dal <megh700004 at yahoo.com> wrote:

> From: Megh Dal <megh700004 at yahoo.com>
> Subject: Having problem to define a subclass, please help me
> To: r-help at stat.math.ethz.ch
> Date: Friday, July 30, 2010, 4:46 PM
> Here I am having problem to define a
> subclass, specially if I define that subclass after defining
> initialize() method for its superclass. Here is my code:
> 
> > setClass("a", representation=list(x="numeric",
> y="numeric"), prototype=list(x=rnorm(10), y=rnorm(10)))
> [1] "a"
> > setMethod("initialize", "a", function(.Object, x, y,
> ...) {
> +               
>              
>    if (length(x) != length(y)) x = y = rep(5,
> 4)
> +               
>              
>    .Object at x = x
> +               
>              
>    .Object at y = y
> +               
>              
>    .Object })
> [1] "initialize"
> > setClass("b", representation=list(x1="character"),
> contains="a")
> Error in .local(.Object, ...) : element 1 is empty;
>    the part of the args list of 'length'
> being evaluated was:
>    (x)
> > 
> 
> Can anyone please point me where I am doing wrong?
> 
> Thanks,
> 
> 
> 
>       
> 






More information about the R-help mailing list