Re: [Rd] SEXPs and slots

From: Patrick Leyshock <ngkbr8es_at_gmail.com>
Date: Mon, 15 Nov 2010 07:45:07 -0800

Very helpful, thank you.

A couple other questions, please:

  1. I've got a function written in C, named "my_c_function". In my R code I call this function, passing to it an INTSXP and a STRSXP, respectively:

   result <- .Call("my_c_function", int_vector, str_vector)

The prototype of "my_c_function" is:

   SEXP my_c_function(SEXP int_vec, SEXP str_vec);

Within my_c_function I am able to extract the values within the integer vector, e.g. I can grab the first value with these lines of code:

   int extracted_value;
   extracted_value = *INTEGER(int_vec);

What I cannot figure out how to do is extract the value from the STRSXP. I'm assuming that I can create a pointer to a character array, then malloc enough memory to hold the value. Is there an analogous operation on "INTEGER" for STRSXPs?

2. Any good references/resources for developing R? Nearly all the documents I've found are for programming R as a user, not as a developer. I have copies of the documentation, which are very helpful, but it'd be helpful to have additional resources to fill in their gaps.

Thank you,

Patrick

On Fri, Nov 12, 2010 at 4:36 PM, Martin Morgan <mtmorgan_at_fhcrc.org> wrote:

> 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
>

        [[alternative HTML version deleted]]



R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Mon 15 Nov 2010 - 16:01:51 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 15 Nov 2010 - 20:20:23 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.

list of date sections of archive