[Rcpp-devel] passing DataFrame from C++ to R

Dirk Eddelbuettel edd at debian.org
Wed Dec 15 21:36:05 CET 2010


On 15 December 2010 at 15:25, WEI ZHANG wrote:
| Sorry I am really new to this.  So it might be a silly question.
| 
| I would like to know if it is possible to create Rcpp::DataFrame in C++ and
| pass it to R.
| 
| From example code, I can see for NumericMatrix, I can do something like:
| 
|     Rcpp::NumericMatrix M(n,n);
| 
|     for (int i=0; i<n; i++) {
| 
|         for (int j=0; j<n; j++) {
| 
|             M(i,j) = i*10+j;
| 
|         }
| 
|     }
| 
|  
| 
| But for Rcpp::DataFrame,  I only see constructor using SEXP, which to my
| understanding,  is to pass data back from R to C++.

And Rcpp::NumericMatrix can be converted automagically to SEXP -- see the
Rcpp-introduction.pdf vignette.

Or, if you wish, peek at the unittests. Here is one for DataFrame (slightlt
rewritten to be more explicit)

     Rcpp::IntegerVector v = Rcpp::IntegerVector::create(1,2,3);
     std::vector<std::string> s(3);
     s[0] = "a";
     s[1] = "b";
     s[2] = "c";
     return Rcpp::DataFrame::create(Rcpp::Named["a"] = v,
			            Rcpp::Named["b"] = s,
			            Rcpp::Named["stringsAsFactors"] = false ); 

| Ideally, I would expect to be able to define C++ data structure for
| data.frame?s row and use vector<row_structure>  to construct Rcpp::DataFrame.

Rcpp::DataFrame is close to how a data frame is built in R itself, it really
is 'just' a list of different columns.  

So if your code logic updates row-wise, you need to cache your data and then
fill your data frame at the end column by column.  Or know beforehand what
columns you will have, create the data frame with these columns and update
one row at a time by sticking each element into its corresponding column.
 
Dirk

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


More information about the Rcpp-devel mailing list