[Rcpp-devel] Iterating an Rcpp::DataFrame

Christian Authmann authmanc at Mathematik.Uni-Marburg.de
Thu Oct 9 14:07:11 CEST 2014


Hello,

this sounds like a newbie question, but I haven't been able to find a 
good answer in Rcpp's documentation, source or testcases..

Say I have an internal data type which I want to wrap/unwrap as a DataFrame.

template<> SEXP wrap(const Foo &foo) {
   Rcpp::DataFrame dataframe;
   for (auto key : foo.keys()) {
     if (key.is_numeric()) {
       Rcpp::NumericVector vec;
       // Fill vector with numbers
       dataframe[ key ] = vec;
     }
     else if (key.is_string()) {
       Rcpp::StringVector vec;
       // Fill vector with strings
       dataframe[ key ] = vec;
     }
     // etc
   }
}

template<> Foo as(SEXP sexp) {
   Rcpp::DataFrame dataframe(sexp);
   // now what?
}


First, I need to iterate over all vectors in the dataframe. I could 
access a vector using
   dataframe["my_key"]
but that would require me to have a list of all the keys/column names 
first. R has a colnames() function, but getting that into an 
Rcpp::Function and calling it seems overly complicated..
I couldn't find anything in Rcpp's source or testcases.. but maybe 
there's a simpler way?


Problem number two, how do I type the vector once I have it? The 
dataframe can contain NumericVector or StringVector (or possibly 
something different). Should I just construct each type, try/catch the 
exceptions and see what fits, or is there a better way to determine the 
type of a SEXP?



Thanks!
Christian

-- 
Christian Authmann
Philipps-Universität Marburg
Fachbereich Mathematik und Informatik
AG Datenbanksysteme
Hans-Meerwein-Straße
D-35032 Marburg


More information about the Rcpp-devel mailing list