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

Dirk Eddelbuettel edd at debian.org
Sun Jul 21 18:28:22 CEST 2013


On 21 July 2013 at 10:56, Wush Wu wrote:
| 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.

What Tal was after was a solution to a) programmatically recurse through a
list and to b) test for list type.  Your snippet, while correct, requires
prior knowledge of 'what is where' -- Tal quest is to find 'what is where'.

Dirk

| 
| Wush
| 
| ----------------------------------------------------------------------
| _______________________________________________
| 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
-- 
Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com


More information about the Rcpp-devel mailing list