[Rcpp-devel] Working with lists of objects

Dirk Eddelbuettel edd at debian.org
Tue Jan 31 00:14:17 CET 2012


On 30 January 2012 at 14:31, Chris DuBois wrote:
| Hi all,
| 
| I'm looking for better ways of dealing with lists of objects.  If I have a
| list s, I want to be able to so an operation like:
| s["hello"][3,3] <- 100
| where I replace the (3,3) element with 100.  Below I show the way I've done
| this so far, but for large lists I don't want to have to manually create new
| objects every time.  Is there a better way to cast every object in an

Those are shallow (and hence "cheap") copies.

| Rcpp::List?

If the data structure you use does not feel right, why not use a different
one?  RcppArmadillo gives you 3d 'cubes' and more ....

Dirk

| Thanks,
| Chris
| 
| library(inline)
| library(Rcpp)
| fx <- cxxfunction(,"",includes=
| '
| // Take the element of s named "hello" and put 100 in the (2,2) element
| Rcpp::List listExample(Rcpp::List s) {
|   Rcpp::NumericMatrix sm = s["hello"];
|   sm(2,2) = 100;
|   s["hello"] = sm;
|   return s;
| }
| RCPP_MODULE(foo){
|   function( "listExample", &listExample ) ;
| }
| ', plugin="Rcpp")
| 
| foo <- Module("foo",getDynLib(fx))
| 
| s <- list(hello=matrix(1:9,3,3))
| foo$listExample(s)
| 
| ----------------------------------------------------------------------
| _______________________________________________
| Rcpp-devel mailing list
| Rcpp-devel at lists.r-forge.r-project.org
| https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
-- 
"Outside of a dog, a book is a man's best friend. Inside of a dog, it is too
dark to read." -- Groucho Marx


More information about the Rcpp-devel mailing list