0

Am porting some C code with a long data structure (40+ items) and was following Ralf Stubner's advice from a previous question: In Rcpp, how to get a user-defined structure from C into R. Things worked great until I tried to add the 21st element:

return Rcpp::wrap(Rcpp::DataFrame::create(Rcpp::Named("institution") = x.institution, // 1st entry
    ...
    Rcpp::Named("number_of_samples") = number_of_samples, // 19th entry
    Rcpp::Named("channel_name") = x.channel_name   ));    // 20th entry

This compiles and runs. When I add any more entries, I get an error: "no matching function for call to 'create'.

    Rcpp::Named("number_of_samples") = number_of_samples, // 19th entry
    Rcpp::Named("channel_name") = x.channel_name,         // 20th entry
    Rcpp::Named("channel_name2") = x.channel_name  ));    // 21st entry

I don't really want "channel_name" listed twice; I just wanted to convince myself that it wasn't a problem with the actual values being entered. Is there some kind of limit in Rcpp code to how big a data frame can be? If not, how would you go about looking for what is causing this error?

In regards to the Comment I gave in the previous question, it is difficult for someone like to me to debug such a problem, because I don't really understand the internal workings of Rcpp. I'm calling "wrap" and "create" functions without really knowing how they do what they do, so it is difficult to know how to fix things when something goes wrong. Rcpp is great; it just seems that you have to program in it by imitation of existing code.

Mark Bower
  • 569
  • 2
  • 16
  • 4
    There is, and that just came up here recently but I didn't immediately find that question. In short, for C++03, we generated these signatures programmatically and 21 is the list. But because a `list` (in both R and Rcpp) can contain other lists, you can simply nest. – Dirk Eddelbuettel Jul 05 '18 at 18:52

1 Answers1

3

Yes, there is a limit.

A nested list is a preferred solution. If you really want to return a list with 40+ elements, you can try something like here or below:

std::vector<std::string> names;

std::vector<SEXP> elements;

// do something with the elements and names

Rcpp::List result(elements.size());

for (size_t i = 0; i < elements.size(); ++i) {
    result[i] = elements[i];
}

result.attr("names") = Rcpp::wrap(names);
// result can be return to R as a list
Qiang Kou
  • 522
  • 4
  • 8