[Rcpp-devel] Access Rcpp::List element

Dirk Eddelbuettel edd at debian.org
Sun May 26 15:42:44 CEST 2013


Simon,

On 26 May 2013 at 15:20, Simon Zehnder wrote:
| Dear Rcpp::Devels and Rcpp::Users,
| 
| I have maybe some trivial questions. 
| 
| 1. If I use 
| 
| 	Rcpp::NumericVector A(someVector),
|     
|     does it reuse memory from someVector for A? From http://cran.r-project.org/web/packages/Rcpp/vignettes/Rcpp-quickref.pdf I would say it does, whereas 
| 	
| 	Rcpp::NumerixVector A(clone(someVector)) 
| 
|     does not.

Yes and yes.  This is all documented.

The first (standard) form uses the "proxy object" instantiation which uses
the _exact same memory used by the object you instantiate from_.  This is
typically done when coming from R with a SEXP, but should work the same for
all other objects.  So someVector and A now use the same "represensation" and
underlying memory: change one, and you changed the other.  The possible side
effect is the cost of the fastest possible instantiation which is also the
most leight-weight.

And hence the need for clone() to create deep copies.
 
| 2. Let us assume I am right in point 1, then my next question would arise towards reusing memory from R: Does it make a difference in C++ if I create a NumericVector with own memory and fill it in a loop or if I reuse memory from R and fill it in a loop? Again, I would say no, as if memory is reused, we usually pass a pointer to the memory tom the Rcpp Object, that has now direct access to this memory (of course this will be different when using parallel code, where memory allocation is essential). 

I don't think I fully understand the question. But let me answer what I am
guestimating you asked: It should not matter.

Besides trying to formulate more concise questions (1. above was good), you
could simply try to __profile__ such suspicions.  That is just about the best
way to settle this.
 
| 3. Imagine I use now an S4 Object from R:
| 
| 	Rcpp::Export SEXP fName (SEXP& data_S4) {
| 	
| 		Rcpp::S4 dataS4O (data_S4);
| 
| 	}
| 
|    If data_S4 contains a list, has the command Rcpp::S4 dataS4(data_S4) already created an Rcpp::List Object from it? If not, how do I proceed to get such a Rcpp::List Object? 

Rcpp::S4 offers you slots. See the documentation, see eg the working examples
in the unit tests and see this example 
   http://gallery.rcpp.org/articles/armadillo-sparse-matrix/
which turns a sparse matrix (an S4 object from the Matrix package) into an
Armdillo sparse matrix:


void convertSparse(S4 mat) {         // slight improvement with two non-nested loops
    IntegerVector dims = mat.slot("Dim");
    IntegerVector i = mat.slot("i");
    IntegerVector p = mat.slot("p");
    NumericVector x = mat.slot("x");
    [...]

You could pick off lists the exact same way. And Lists inside Lists inside S4
elements ...  SEXP nesting works as it does in R.

Finally, if I may: You have a bit of a tendency to come to the list, ask a
question, and then to disappear [eg your recent Armadillo RNG question].  

For more mutually beneficial discourse, I would appreciate it if you could
reply, extend the examples or generally clarify whereever you find something
too brief.  Of course, I don;t mean to suggest that a one-line 'me too' is
beneficial, but it would be nice to turn this into a dialogue rather than a
free answering system for whatever question bothers you right now :)  Also
feel free to help others on the list.  You obviously already understand a lot
of the material rather well...

Regards, Dirk

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


More information about the Rcpp-devel mailing list