[Rd] Dealing with R list objects in C/C++

Martin Morgan mtmorgan at fhcrc.org
Thu Jan 27 22:16:51 CET 2011


On 1/27/2011 1:03 PM, Wayne.Zhang at barclayscapital.com wrote:
> Many thanks for the quick reply Martin, your code works as expected.  Next I'd like to retrieve heterogeneous data from an SEXP object (let's just pretend it's the same type as the one what I'm constructing).  I'm sure the relevant APIs are defined in Rinternals.h, do we have API documentations for this header file somewhere?
Hi Wayne -- Your best bet might be sections 5 and 6 of

     RShowDoc("R-exts")

or the books Dirk mentioned; see also Rdefines.h. Martin
> @Dirk: thanks for your help too.  I'm doing something very simple at the moment, so I prefer not to bring in Rinside/Rcpp if possible.
>
> Thanks again,
> Wayne
>
>
> -----Original Message-----
> From: Martin Morgan [mailto:mtmorgan at fhcrc.org]
> Sent: Wednesday, January 26, 2011 10:04 PM
> To: Zhang, Wayne: IT (NYK)
> Cc: r-devel at r-project.org
> Subject: Re: [Rd] Dealing with R list objects in C/C++
>
> On 01/26/2011 02:56 PM, Wayne.Zhang at barclayscapital.com wrote:
>> Hi,
>>
>> I'd like to construct an R list object in C++, fill it with relevant data, and pass it to an R function which will return a different list object back.  I have browsed through all the R manuals, and examples under tests/Embedding, but can't figure out the correct way.  Below is my code snippet:
>>
>>      #include<Rinternals.h>
>> // Rf_initEmbeddedR and other setups already performed
>>
>>      SEXP arg, ret;
>>
>>      // this actually creates a pairlist.  I can't find any API that creates a list
>> PROTECT(arg = allocList(3));
> Allocate a list of length 3 via SEXPTYPE VECSXP
>
>        PROTECT(arg = allocVector(VECSXP, 3));
>
>> // I want the first element to be type integer, second double, and third a vector.
>>      INTEGER(arg)[0]  = 1;            //<- runtime exception: "INTEGER() can only be applied to a 'integer', not a 'pairlist'
> set the first element of the list to an integer vector of length 1, and
> assign a value
>
>        SET_VECTOR_ELT(arg, 0, allocVector(INTSXP, 1));
>        INTEGER(VECTOR_ELT(arg, 0))[0] = 1
>
> or more succinctly
>
>        SET_VECTOR_ELT(arg, 0, ScalarInteger(1));
>
>>      REAL(arg)[1] = 2.5;               // control never reached here
> and the second element
>
>        SET_VECTOR_ELT(arg, 1, ScalarReal(2.5));
>
>>      VECTOR_PTR(arg)[2] = allocVector(REALSXP, 4);
> and for the third allocate a REALSXP and then fill
>
>        SET_VECTOR_ELT(arg, 2, allocVector(REALSXP, 4));
>
> next lines should be ok as REAL(VECTOR_ELT(arg, 2))[0] = 10.0; or with
> less typing as
>
>        double *x = REAL(VECTOR_ETL(arg, 2));
>        x[0] = 10.0; x[1] = 11.0; x[2] = 12.0; x[3] = 13.0;
>
>>      REAL(VECTOR_PTR(arg)[2])[0] = 10.0;
>>      REAL(VECTOR_PTR(arg)[2])[1] = 11.0;
>>      REAL(VECTOR_PTR(arg)[2])[2] = 12.0;
>>      REAL(VECTOR_PTR(arg)[2])[3] = 13.0;
>>
>>      PROTECT(call = lang2(install(entryPoint.c_str()), arg));
> not sure where entryPoint.c_str() is coming from, but
>
>       PROTECT(call = lang2(install("fun"), arg));
>
> with some debate about whether install("fun") should be PROTECT'ed.
>
>> ret = R_tryEval(call, R_GlobalEnv,&errorOccurred);
> likely PROTECT(ret = ...) while checking errorOccurred, etc.
>
> Hope that helps,
>
> Martin
>
>>
>> I'll be grateful if you can point me to any online docs/samples.
>>
>> Thanks in advance,
>> Wayne
>>
>> _______________________________________________
>>
>> This e-mail may contain information that is confidential, privileged or otherwise protected from disclosure. If you are not an intended recipient of this e-mail, do not duplicate or redistribute it by any means. Please delete it and any attachments and notify the sender that you have received it in error. Unless specifically indicated, this e-mail is not an offer to buy or sell or a solicitation to buy or sell any securities, investment products or other financial product or service, an official confirmation of any transaction, or an official statement of Barclays. Any views or opinions presented are solely those of the author and do not necessarily represent those of Barclays. This e-mail is subject to terms available at the following link: www.barcap.com/emaildisclaimer. By messaging with Barclays you consent to the foregoing.  Barclays Capital is the investment banking division of Barclays Bank PLC, a company registered in England (number 1026167) with its registered off
> i!
>>   ce at 1 Churchill Place, London, E14 5HP.  This email may relate to or be sent from other members of the Barclays Group.
>> _______________________________________________
>>
>> 	[[alternative HTML version deleted]]
>>
>> ______________________________________________
>> R-devel at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-devel
>


-- 
Dr. Martin Morgan, PhD
Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N.
PO Box 19024 Seattle, WA 98109



More information about the R-devel mailing list