[Rcpp-devel] is<T>( SEXP )

romain at r-enthusiasts.com romain at r-enthusiasts.com
Wed Jul 17 12:15:35 CEST 2013


Hello,

I've just commited some code that will help us identify if a given R 
object (a SEXP) can be seen as a given c++ type (e.g. IntegerVector, etc 
...)

Works like this:

SEXP x ; // a SEXP from somewhere, or an RObject
is<IntegerVector>( x ) ;
is<int>( x ) ;
is<DataFrame>( x ) ;

There are several applications of this.

Sometimes we get heterogeneous lists from R, perhaps recursive, and 
what you do with each element can depend on what it actually is. e.g. at 
useR Tal wanted to deal with dendrogram and these are nested lists.

So we could imagine :

void process( List x ){
    for( int i=0; i<x.size(); i++){
       if( is<List>( x[i] ) ){
          process( x[i] ) ;
       } else if( is<NumericVector>( x[i] ) ) {
          // do something else
       }
}

This way we can be expressive in a C++ way. Before this addition, we 
would have to go back to using SEXP and TYPEOF and know about VECSXP, 
etc ... and the less users have to manipulate these, the better.

The other application I have in mind is dispatch. In modules for 
example the sort of code we have to write to dispatch between 
constructors etc ... is again going back to manipulate C arrays of SEXP. 
So manipulating SEXP*. I'm hoping that is will make this easier.

dispatching could also happen in attributes. Why not having something 
like this:

// [[Rcpp::export]]
void foo( NumericVector x) {
     // do some stuff
}

// [[Rcpp::export]]
void foo( IntegerVector x) {
     // do some other stuff
}

This would involve some work in the way attributes work. But I can 
defintely see the value of this. Comments. JJ ?

Romain






More information about the Rcpp-devel mailing list