[Rcpp-devel] getting ncol(DF) in Rcpp

Silkworth,David J. SILKWODJ at airproducts.com
Mon Jun 27 19:30:14 CEST 2011

You guys know I am here just to give you a chuckle. 
I wanted to build a function passing just a dataframe to Rcpp.  In order
to use this dataframe, I need to know how many columns it has at
runtime.  My attempts at getting this ncol information were thwarted on
several counts.  The Dimension class appears to only work on STL
containers, which Rcpp::DataFrame is not.  I resorted to the Environment
facility to attempt a feeble-minded RInside, (since I can't understand
RInside anyway).

Environment base("package:base");
Function ncol = base["ncol"];
Rcpp::NumericVector test(1);

This fails to compile with the following error:
error: cannot convert 'SEXPREC*' to

However, just short of sending another single element vector with this
information as an argument to Rcpp I tried the following, AND IT WORKED!

(My debug technique is to send items back to R for inspection.  This is
just some test code to show that an integer value of myNames.size() will
be useful as a proxy for ncol(DF) in further code development.)

src <- '
Rcpp::DataFrame myDF=(arg1);
Environment base("package:base");
Function names = base["names"];
Rcpp::CharacterVector myNames(names(myDF));
Rcpp::NumericVector ncol(1);

 fun <- cxxfunction(signature(arg1 = "numeric"),
 src, plugin = "Rcpp")


Okay, how's that for a laugher.

In my real case I am using the same dataframe that I needed to clean up
in my 'redimension' chain.  My solution there works quite fine.  Now I
have yet to decompose this dataframe back into vectors and a matrix to
enable entries to be accessed in Rcpp.  But at least I have the
dimensions for the matrix now.

It takes about 3 seconds for R to extract a matrix based on
DF[,3:ncol(DF)] on a dataframe with 46,000 rows.  I am counting on Rcpp
code to execute this more efficiently.  One could argue that I should
never have left Rcpp in the first place.  But that is another story.

More information about the Rcpp-devel mailing list