[Rcpp-devel] Filling a list with iterator, or setting names from CharacterVector/SEXP?

Dirk Eddelbuettel edd at debian.org
Sat Sep 1 17:13:12 CEST 2012


On 31 August 2012 at 23:44, Christian Gunning wrote:
| On Fri, Aug 31, 2012 at 5:50 AM, Dirk Eddelbuettel <edd at debian.org> wrote:
| >
| > Yes -- I needed this the other day to return what I got in C++ as a
| > std::vector<std::string> vecstr. I used something like
| >
| >          Rcpp::CharacterVector charvec( vecstr.begin(), vecstr.end() )
| 
| Ok, I was wondering about std::vector.  But how do I go the other way
| and get a std::vector<std::string> vecstr from a CharacterVector?
| Can I use as<std::string>() and then iterate over the CharacterVector?

Sure, works via   Rcpp::as< std::vector< std::string> >(foo)

| My use-case is access-by-name.  So in this example I want, for example:
|  R> f(x)
|  [["Moe"]]
|  [1] 1
| 
|  [["Curly"]]
|  [1] 2
| 
| Of course, I can just hand in list(Moe=1, Curly=2), but then I still
| don't know how to get the names of the list out of the list into a
| CharacterVector or  std::vector<std::string>.  Does this sort of make
| sense?

I am not sure what it is that you want but if you look e.g. at RcppExamples
and its examples related to parameter passage in and out, you see that we can
pick _arbitrary_ objects in and out of Rcpp::Lists by name.   Does that help?

Dirk

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


More information about the Rcpp-devel mailing list