[Rcpp-devel] Working with lists of objects

Chris DuBois chris.dubois at gmail.com
Mon Jan 30 23:31:25 CET 2012


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
Rcpp::List?

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)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20120130/210962ba/attachment.htm>


More information about the Rcpp-devel mailing list