2
votes

In all Rcpp examples that I could find, one must know the type of DataFrame columns, then you can extract column into vector, e.g.:

// construct the data.frame object
Rcpp::DataFrame DF = Rcpp::DataFrame(Dsexp);
// and access each column by name
Rcpp::IntegerVector a = DF["a"];
Rcpp::CharacterVector b = DF["b"];
Rcpp::DateVector c = DF["c"];

In R, one could find column types using sapply() function and class or typeof.

However, I couldn't find the external API in Rcpp responsible for type checking. Is there a way to find column type programmatically?

2
It seems you need to apply TYPEOF() and R_data_class() over each column; they return a SEXPTYPE and SEXP (STRSXP) respectively. - alexis_laz
Can the downvoter explain? - Oleg Shirokikh

2 Answers

4
votes

Thanks to Dirk's comment, I found the solution in RcppGallery article:

Dynamic Wrapping and Recursion with Rcpp

TYPEOF and Rf_isFactor should be sufficient to determine the column types of DataFrame. Rinternals.h define the following types, which would be returned by TYPEOF:

#define INTSXP      13    /* integer vectors */
#define REALSXP     14    /* real variables */
#define CPLXSXP     15    /* complex variables */
#define STRSXP      16    /* string vectors */
#define DOTSXP      17    /* dot-dot-dot object */
#define ANYSXP      18    /* make "any" args work. */
#define VECSXP      19    /* generic vectors */
...
3
votes

You are not making a lot of sense to me.

Your example is on names of the columns, which is what works and what is documented.

But then you wish for a pony and selection on type. Well, I have bad news for you: C++ generally does not have reflexivity. So we need to supply extra information. For which we often use names. Which gets us back to the previous case.

Trust me: it would be nice to do better. I am not sure I see how we can though.