[R] S4 class slot type S4 class

Martin Morgan m@rt|n@morg@n @end|ng |rom ro@we||p@rk@org
Mon May 21 16:37:45 CEST 2018



On 05/21/2018 12:06 AM, Glenn Schultz wrote:
> All,
> 
> I am considering creating an S4 class whose slots (2) are both S4 
> classes.  Since an S4 slot can be an S3 class I figure this can be done. 
>   However, the correct syntax of which I am unsure.  Reviewing the docs 
> I have come to the following conclusion:
> 
> SetClass('myfoo',
>                    slots = (foo1, foo2))
> 
> Without a type I believe each slot is .Data.  A get method on the above 
> class slots would return say foo1 which will have all methods and 
> generics belonging to foo1 class.  Is this the correct approach?

Suppose you have two classes

   .A = setClass("A", slots = c(x = "numeric"))
   .B = setClass("B", slots = c(y = "numeric", z = "numeric"))

A third class containing these would be

   .C = setClass("C", slots = c(a = "A", b = "B"))

where names of the slot argument are the slot names, and the character 
strings "A", "B" are the type of object the slot will store.

 > .C()
An object of class "C"
Slot "a":
An object of class "A"
Slot "x":
numeric(0)


Slot "b":
An object of class "B"
Slot "y":
numeric(0)

Slot "z":
numeric(0)


 > .C(a = .A(x = 1:2), b = .B(y = 2:1, z = 1:2))
An object of class "C"
Slot "a":
An object of class "A"
Slot "x":
[1] 1 2


Slot "b":
An object of class "B"
Slot "y":
[1] 2 1

Slot "z":
[1] 1 2



Martin Morgan

> 
> Best,
> Glenn
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.


This email message may contain legally privileged and/or...{{dropped:2}}




More information about the R-help mailing list