[Rcpp-devel] RInside input pointers to arrays

Dirk Eddelbuettel edd at debian.org
Thu Oct 15 00:39:23 CEST 2015


On 14 October 2015 at 15:12, Allison Irvine wrote:
| I am trying to figure out how to avoid creating a copy of an array in 
| c++ to a Rcpp::NumericVector or std::vector in order to use the data in 
| an embedded R instance (RInside object). It is clear that one can copy 
| an array in C++ to a Rcpp::NumericVector, then assign a variable in the 
| embedded R object, for example:
| 
| 
| double myArray[5] = {1,2,3,4,5};
| Rcpp::NumericVector myNumericVector(5);
| for (int i=0; i<5; i++) {
|          myNumericVector[i] = myArray[i];
| }
| RInside R(argc, argv);
| R["x"] = myNumericVector;
| 
| However, I would like to avoid having to create a copy of the array. Is 
| there a way to assign the "x" variable in the above example using 
| pointers to the C++ array "myArray"?
| 
| Also, I am interested in doing the inverse of that. Is it possible to 
| take the output of the embedded R code called by R.parseEval() and have 
| it return the values to a pre-allocated array in c++?

Yes, there are constructors that take, say, an iterator the vector and a
dimension.  In general, you do NOT need copies.

Search the list, the Rcpp Gallery and StackOverflow for examples. This all
has been discussed before.

Dirk


-- 
http://dirk.eddelbuettel.com | @eddelbuettel | edd at debian.org


More information about the Rcpp-devel mailing list