Skip to content
Prev 2723 / 10988 Next

[Rcpp-devel] add new components to list without specifying list size initially

Thanks for your advice, I now understand how to manipulate one-level lists:

fn <- cxxfunction(signature(l_in="list"),
                  body='
using namespace Rcpp;
List l(l_in);
IntegerVector lf = l["foo"];
CharacterVector lb = l["bar"];
for(int i=0; i<lf.size(); ++i)
  Rprintf("l[%s][%i] %i\\n", "foo", i, lf[i]);
for(int i=0; i<lb.size(); ++i)
  Rprintf("l[%s][%i] %s\\n", "bar", i, std::string(lb[i]).c_str());
', plugin="Rcpp", verbose=TRUE)
z <- fn(list(foo=c(1,2,3,4),bar=c("bar1","bar2")))

But what about 2-level lists? Why the following code doesn't compile?

fn <- cxxfunction(signature(l_in="list"),
                  body='
using namespace Rcpp;
List l(l_in);
List lf(l["foo"]);
', plugin="Rcpp", verbose=TRUE)
z <- fn(list(foo=list(bar=1)))

And what the following message mean? "error: call of overloaded
?Vector(Rcpp::internal::generic_name_proxy<19>)? is ambiguous"

I had a look at "runit.Vector.R" on r-forge, but couldn't find any test
involving 2-level (or more) lists, although on SO in June 2010 (
http://stackoverflow.com/questions/3088650/how-do-i-create-a-list-of-vectors-in-rcpp/3088744#3088744),
you said that it should work.

I checked that I can create a 2-level list, but the code below doesn't
compile if I uncomment the last Rprintf line:

fn <- cxxfunction(signature(),
                  body='
using namespace Rcpp;
IntegerVector vi(2);
vi[0] = 2;
vi[1] = 8;
List ll = List::create(Named("bar")=vi);
Rprintf("ll.size %i\\n", ll.size());
List l = List::create(Named("foo")=ll);
Rprintf("l.size %i\\n", l.size());
//Rprintf("l.ll.size %i\\n", l["foo"].size());
return l;
', plugin="Rcpp", verbose=TRUE)
print(fn())

Thus once again I'm stuck, but if I know how to access 2-level lists, I
think I will be able to go back to my original problem, and stop sending
emails on this mailing list ;)
On Fri, Aug 12, 2011 at 8:09 AM, Dirk Eddelbuettel <edd at debian.org> wrote:

            
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20110812/684966d5/attachment-0001.htm>