[Rcpp-devel] Multidimensional List (List of Lists)

Dirk Eddelbuettel edd at debian.org
Mon Mar 25 04:01:40 CET 2013


On 24 March 2013 at 23:35, Marco wrote:
| Hi, I being looking around but couldn't find a way to create a list of a 
| list inside Rcpp. Is that possible?
| 
| So far I tried:
| 
| List ret1;
| List ret2;
| ret1["name1"] = resultsMatrix;
| ret2["name2"] = covarMatrix;
| List ret;
| ret["samples"] = ret1;
| ret["covmatrix"] = ret2;
| 
| 
| But no sucess.

a) You posted twice. That's close to spamming.

b) Your example is incomplete and non-reproducible. Not a way to earn brownie
points either.


De minimis example:

R> sourceCpp("/tmp/reclist.cpp")
R> reclist()
[[1]]
[[1]][[1]]
[1] 10 20

[[1]][[2]]
foo bar 
 20  30 


[[2]]
[[2]][[1]]
[1] 10 20

[[2]][[2]]
foo bar 
 20  30 


R> 

Using code from the first unit test example I found on List, and making it
two level:


#include <Rcpp.h>

using namespace Rcpp;

// [[Rcpp::export]]
List reclist(){
    List a(2);
    List output(2);
    output[0] = IntegerVector::create( 10, 20 ) ;
    output[1] = IntegerVector::create(_["foo"] = 20,
				      _["bar"] = 30 ) ;
    a[0] = output;
    a[1] = output;
    return a;
}


Dirk

-- 
Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com  


More information about the Rcpp-devel mailing list