[Rcpp-devel] Help with accessing and manipulating List objects

Kevin Ushey kevinushey at gmail.com
Fri Aug 16 20:04:32 CEST 2013


Just a suggestion re: recursion with Rcpp, yet another nice way we can
recurse is through using the sugar lapply:

-----

#include <Rcpp.h>
using namespace Rcpp;

// [[Rcpp::export]]
RObject add_attr(RObject x_) {
  RObject x = clone(x_);
  if (is<List>(x)) {
    x = wrap( lapply( as<List>(x), add_attr ) );
  } else {
    x.attr("type") = "Other";
  }
  return x;
}

/*** R
add_attr( list(x=list(a=1, b=2, c=3), y=list( list( d=4 ) ) ) )
*/

-----

If we want to operate on specific elements as well, we can use is<T> and
as<T> together, and then operate on objects as needed.

It lets us avoid an explicit for loop, and 'feels' more like R.

-Kevin
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20130816/56b9f117/attachment.html>


More information about the Rcpp-devel mailing list