Hello,<br>I need to create a list and then fill it sequentially by adding components in a for loop. Here is an example that works:<br><br>library(inline)<br>src <- '<br>Rcpp::List mylist(2);<br>for(int i=0; i<2; ++i)<br>
mylist[i] = i;<br>mylist.names() = CharacterVector::create("a","b");<br>return mylist;<br>'<br>fun <- cxxfunction(body=src, plugin="Rcpp")<br>print(fun())<br><br>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:<br>
<br>Rcpp::List mylist;<br>CharacterVector names = CharacterVector::create("a", "b");<br>
for(int i=0; i<2; ++i){<br> mylist.add(names[i], IntegerVector::create());<br>
mylist[names[i]].push_back(i);<br>}<br>
return mylist;<br>
<br>Do you know how I could achieve this? Thanks.<br>