Skip to content

Dealing with R list objects in C/C++

5 messages · Dirk Eddelbuettel, Martin Morgan, Wayne.Zhang at barclayscapital.com

#
Hi Wayne,
On 26 January 2011 at 17:56, 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));
| 
| // 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'
|     REAL(arg)[1] = 2.5;               // control never reached here
| 
|     VECTOR_PTR(arg)[2] = allocVector(REALSXP, 4);
|     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));
| 
| ret = R_tryEval(call, R_GlobalEnv, &errorOccurred);
| 
| 
| I'll be grateful if you can point me to any online docs/samples.

This is a non-trivial problem when the use the C API provided by R. It is all
documented, but you need to study the 'Writing R Extensions' in some detail,
as well as maybe 'R Programming' by Gentleman and/or 'Software for Data
Analysis' by Chambers.

But there is another API you can use. It is provided by RInside (to embed R
inside C++) which uses Rcpp (for R and C++ integration).  Install those two
packages from CRAN, and then drop the few lines below as a file, say,
wayne.cpp in the examples/standard/ directory of RInside. Saying 'make wayne'
will build an executable, using proper flags and linker options, and you can
run that:

edd at max:~/svn/rinside/pkg/inst/examples/standard$ make wayne
g++ -I/usr/share/R/include -I/usr/local/lib/R/site-library/Rcpp/include -I"/usr/local/lib/R/site-library/RInside/include" -O3 -pipe -g -Wall    wayne.cpp  -L/usr/lib64/R/lib -lR  -lblas -llapack -L/usr/local/lib/R/site-library/Rcpp/lib -lRcpp -Wl,-rpath,/usr/local/lib/R/site-library/Rcpp/lib -L/usr/local/lib/R/site-library/RInside/lib -lRInside -Wl,-rpath,/usr/local/lib/R/site-library/RInside/lib -o wayne
edd at max:~/svn/rinside/pkg/inst/examples/standard$ ./wayne 
Showing list content:
L[0] 1
L[1] 2.5
L[2][0] 10
L[2][1] 11
Showing list content:
L[0] 42
L[1] 42
L[2][0] 10
L[2][1] 42
edd at max:~/svn/rinside/pkg/inst/examples/standard$ 

The code a list as you spec'ed with int, double and vector. The list is shown
on stdout, then passed to R, transformed by R and shown again at the C++ level.

Questions on RInside and Rcpp are welcome on the rcpp-devel list.

Hope this helps,  Dirk

-----------------------------------------------------------------------------
#include <RInside.h>                    // for the embedded R via RInside

void show(const Rcpp::List & L) {
    // this function is cumbersome as we haven't defined << operators
    std::cout << "Showing list content:\n";
    std::cout << "L[0] " << Rcpp::as<int>(L[0]) << std::endl;
    std::cout << "L[1] " << Rcpp::as<double>(L[1]) << std::endl;
    Rcpp::IntegerVector v = Rcpp::as<Rcpp::IntegerVector>(L[2]);
    std::cout << "L[2][0] " << v[0] << std::endl;
    std::cout << "L[2][1] " << v[1] << std::endl;
}

int main(int argc, char *argv[]) {

    // create an embedded R instance
    RInside R(argc, argv);               

    Rcpp::List mylist(3);
    mylist[0] = 1;
    mylist[1] = 2.5;
    Rcpp::IntegerVector v(2); v[0] = 10; v[1] = 11; // with C++0x we could assign directly
    mylist[2] = v;
    show(mylist);

    R["myRlist"] = mylist;
    std::string r_code = "myRlist[[1]] = 42; myRlist[[2]] = 42.0; myRlist[[3]][2] = 42; myRlist";
    
    Rcpp::List reslist = R.parseEval(r_code);
    show(reslist);

    exit(0);
}
-----------------------------------------------------------------------------
#
On 01/26/2011 02:56 PM, Wayne.Zhang at barclayscapital.com wrote:
Allocate a list of length 3 via SEXPTYPE VECSXP

      PROTECT(arg = allocVector(VECSXP, 3));
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));
and the second element

      SET_VECTOR_ELT(arg, 1, ScalarReal(2.5));
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;
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.
likely PROTECT(ret = ...) while checking errorOccurred, etc.

Hope that helps,

Martin
i!

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

@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:
Allocate a list of length 3 via SEXPTYPE VECSXP

      PROTECT(arg = allocVector(VECSXP, 3));
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));
and the second element

      SET_VECTOR_ELT(arg, 1, ScalarReal(2.5));
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;
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.
likely PROTECT(ret = ...) while checking errorOccurred, etc.

Hope that helps,

Martin
i!

  
    
#
On 1/27/2011 1:03 PM, Wayne.Zhang at barclayscapital.com wrote:
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