[Rd] SEXPs and slots

Martin Morgan mtmorgan at fhcrc.org
Sat Nov 13 01:36:56 CET 2010


On 11/12/2010 02:31 PM, Patrick Leyshock wrote:
> Hello,
> 
> I've created this class:
> 
> setClass("example",
>    representation  (
>           size = "numeric",
>           id    = "character"
>    )
> )
> 
> Suppose I create a new instance of this class:
> 
>> x <- new("example", 4, "id_value")
> 
> This creates an S4 object with two slots.  Am I correct in thinking that
> slots are "filled" by SEXPs?

Hi Patrick --

If I

> eg = new("example", size=4, id="id_value")

(note the named arguments) and take a peak at the str'ucture of eg, I see

> str(eg)
Formal class 'example' [package ".GlobalEnv"] with 2 slots
  ..@ size: num 4
  ..@ id  : chr "id_value"

so the @size slot is a numeric vector of length 1 containing the value
4. One doesn't really have to know the detailed representation, but one
can find out from

> .Internal(inspect(eg))
@df70e48 25 S4SXP g0c0 [OBJ,NAM(2),gp=0x10,ATT]
ATTRIB:
  @df70ef0 02 LISTSXP g0c0 []
    TAG: @769258 01 SYMSXP g1c0 [MARK] "size"
    @c0f6db8 14 REALSXP g0c1 [NAM(2)] (len=1, tl=0) 4
    TAG: @15b0228 01 SYMSXP g1c0 [MARK,NAM(2)] "id"
    @c0f6178 16 STRSXP g0c1 [NAM(2)] (len=1, tl=0)
      @12341c80 09 CHARSXP g0c2 [gp=0x20] "id_value"
    TAG: @607ce8 01 SYMSXP g1c0 [MARK,NAM(2),gp=0x4000] "class"
    @c0f6d58 16 STRSXP g0c1 [NAM(2),ATT] (len=1, tl=0)
      @96ed08 09 CHARSXP g1c1 [MARK,gp=0x21] "example"
    ATTRIB:
      @df70fd0 02 LISTSXP g0c0 []
	TAG: @624f70 01 SYMSXP g1c0 [MARK,NAM(2)] "package"
	@c0f6d88 16 STRSXP g0c1 [NAM(2)] (len=1, tl=0)
	  @67f5e0 09 CHARSXP g1c2 [MARK,gp=0x21,ATT] ".GlobalEnv"

that the 'eg' object is an S4SXP with an attribute that is a LISTSXP.
The LISTSXP has elements that are tagged with SYMSXP representing the
slot name, and values that are REALSXP (for 'size') or STRSXP (for
'id'). The LISTSXP attribute itself has an attribute, which contains
information about the package where the class is defined. With these
hints one can see through the S4 interface to the underlying implementation

> attributes(eg)
$size
[1] 4

$id
[1] "id_value"

$class
[1] "example"
attr(,"package")
[1] ".GlobalEnv"

But probably you have a specific goal in mind, and this is too much
information...

Martin

> 
> Thanks, Patrick
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel


-- 
Computational Biology
Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N. PO Box 19024 Seattle, WA 98109

Location: M1-B861
Telephone: 206 667-2793



More information about the R-devel mailing list