Hello,
I need to create a list and then fill it sequentially by adding components
in a for loop. Here is an example that works:
library(inline)
src <- '
Rcpp::List mylist(2);
for(int i=0; i<2; ++i)
mylist[i] = i;
mylist.names() = CharacterVector::create("a","b");
return mylist;
'
fun <- cxxfunction(body=src, plugin="Rcpp")
print(fun())
But what I really want is to create an empty list and then fill it, that is
without specifying its number of components before hand... This is because I
don't know in advance at which step of the for loop I will need to create a
new component. Here is an example, that obviously doesn't work, but that
should show what I am looking for:
Rcpp::List mylist;
CharacterVector names = CharacterVector::create("a", "b");
for(int i=0; i<2; ++i){
mylist.add(names[i], IntegerVector::create());
mylist[names[i]].push_back(i);
}
return mylist;
Do you know how I could achieve this? Thanks.
_______________________________________________
Rcpp-devel mailing list
[email protected]
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel