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

Wush Wu wush978 at gmail.com
Sun Jul 21 04:56:45 CEST 2013


Hi Tal,

> That is, say that we have x as a List, how do we go about fetching its
attr?
> I tried:
> x[1].attr("type")
> x.attr[1]("type")
> But none seemed to have worked. Any suggestions there?

Here is a script to show you how to access them:

```r
x <- list(a = 1, b = 2, c = list(ca = 3, cb = 4, 5), 6)
attr(x[[1]], "type") = "fun"
attr(x[[3]][[1]], "type") = "fun"
x

library(inline);library(Rcpp)
f <- cxxfunction(sig=c(Rx="list"), plugin="Rcpp", body='
 List x(Rx);
IntegerVector a(wrap(x[0]));
 Rcout << as<std::string>(a.attr("type")) << std::endl;
List c(wrap(x[2]));
 IntegerVector ca(wrap(c[0]));
Rcout << as<std::string>(ca.attr("type")) << std::endl;
 ')
f(x)
```

I hope it helps.

Wush
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20130721/0a7d0b6a/attachment.html>


More information about the Rcpp-devel mailing list