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

Christian Gunning xian at unm.edu
Fri Aug 31 08:53:50 CEST 2012


To clarify a little, I can avoid most of this hassle by constructing
my named lists in R.  I would like to compute on the names of those
lists on the C++ end, though -- things akin to R's
which(names(mylist)=='myname') and easily looping through names.
-Christian

On Thu, Aug 30, 2012 at 9:52 PM, Christian Gunning <xian at unm.edu> wrote:
>
>
> using namespace Rcpp ;
> class World {
>     public:
>         World(SEXP my_) : my(my_) {
>             mynames = my.attr("dimnames");
>             CharacterVector rownames = mynames[0];
>             nrows = rownames.size();
>             myrows = Rcpp::List(0);
>             for (int i=0; i<nrows; i++) {
>                 Rcpp::CharacterVector tmpname(rownames[i]);
>                 std::string thisname = as<std::string>(tmpname);
>                 myrows[thisname] = i;
>             }
>         }
>        Rcpp::List myrows;
>        Rcpp::List getrows() {
>            return myrows;
>       }
>
>     private:
>         Rcpp::NumericMatrix my;
>         Rcpp::List mynames;
>         int  nrows;
> };



-- 
A man, a plan, a cat, a ham, a yak, a yam, a hat, a canal – Panama!


More information about the Rcpp-devel mailing list