[Rcpp-devel] Copying a NumericVector from a list within a list

Dirk Eddelbuettel edd at debian.org
Sun Apr 13 17:39:11 CEST 2014


On 13 April 2014 at 11:18, Krzysztof Sakrejda wrote:
| Sorry, accidental gmail send... here's what I meant:
| 
| On Sat, Apr 12, 2014 at 11:06 PM, Dirk Eddelbuettel <edd at debian.org> wrote:
| > What can work as a single expression in R sometimes needs to be disentangled
| > at the C++ level to help the compiler find its way through all the templates.
| >
|  [snip]
| >
| > // [[Rcpp::export]]
| > NumericVector fromList(List outer, std::string name) {
| >   List inner = outer[0];                // 0-based offsets in C++
| >   return inner["p"];
| > }

First off, you repeat the same error I made :) and omit 'name'. It should be

     return inner[name];

which works the same way but uses the second variable.
 
|  I've noticed (I think...) having to do this sort of thing, but I want
|  to ask a question to clarify: is it possible to have the above code
|  work, but if you instead try:
| 
|  NumericVector fromList(List outer, std::string name) {
|      return (outer[0])["p"];
|  }
| 
| or even:
| 
|  NumericVector fromList(List outer, std::string name) {
|      NumericVector vec = (outer[0])["p"];
|      return vec;
|  }
| 
| It will fail to compile?  That's what you mean by needing to "help the
| compiler"?  If so, is there some terminology for these sorts of problems?

Did you try it?   

Well, Dion did and it didn't work for him, hence his question to the list.

I did too, and it didn't work which is why I posted the answer I posted, and
made the comment I made. 

It is of course possible to refine and complete the template until it
automagically hits any and all combinations of types.  That is also extremely
tedious so nobody has done it yet.   

In general, things are better than they used to be. But for now the
'two-step' process may the best you got. Patches are always welcome...

Dirk


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


More information about the Rcpp-devel mailing list