Skip to content
Prev 445 / 10988 Next

[Rcpp-devel] Returning a named (and classed?) list from an RcppExport SEXP function

Le 12/03/10 22:00, Douglas Bates a ?crit :
I agree. Nobody would do something like this in R:

 > pairlist( foo = 1, bar = 2 )

so I would be in favour of giving less exposure to the Pairlist class. 
IIRC the only reason why Dirk uses them is because of the convenience of 
creating the object in one call... so we just need to have similar 
semantics with the List class.
This should enable c++0x :

RCPP_CXX0X="yes" R CMD INSTALL Rcpp

(we don't use the PKG_CXXFLAGS for this because otherwise R CMD check is 
unhappy about non portable flags)


Ah yes. 5 here is arbitrary, and it would not be too hard to make it 10, 
20 or something. Now that I am on OSX, I am stuck with gcc 4.2 :-( so no 
c++0x for me anymore (well only when I use my fedora machine). I took 
the precaution of wrapping the code bloat code into pseudo xml :

/* <code-bloat> */

/* </code-bloat> */

but wrote that manually. Should not be hard to come up with some R code 
to generate the code bloat... maybe tomorrow.
We outsource this to the as.list function of R. This is probably not the 
easiest piece of code, and it surely deserve some more documentation. In 
a nutshell :

The constructor :

List(SEXP) aka Vector<VECSXP>( SEXP )

calls r_cast<VECSXP> which calls r_true_cast if the object is not a 
VECSXP, which looks like this:

template<> SEXP r_true_cast<VECSXP>(SEXP x){
	return convert_using_rfunction(x, "as.list" ) ;
}

This is somewhat lazy and we could to some switch( TYPEOF( )) to keep 
some things internal for the easy cases (LISTSXP, EXPRSXP, LANGSXP) and 
use as.list for the other cases to avoid the price of the round trip to 
the R side.
ListInitialization might not be what you think. It is only there to 
support this notation.

NumericVector x(4) ;
x = 1.0, 2.0, 3.0, 4.0 ;

This is a trick we borrow from blitz++

but C++0x gives this notation that I find much nicer:

NumericVector x = {1.0, 2.0, 3.0; 4.0} ;