[Rcpp-devel] Rcpp version of c( )

Søren Højsgaard sorenh at math.aau.dk
Wed Jan 22 01:37:00 CET 2014

Hi Kevin,

Thanks for your reply. I was only introduced to C++11 last week (my fault!); it seems that everybodys life becomes much easier once R-packages can be made with that...

I think many Rcpp-friends would welcome a version of c( ). One view is of course that it should be as general as R's c( )-function. A more pragmatic view is a version of c() that could concatenate (a) two lists and (b) two vectors (where each can be integers, doubles, characters and complex, independently of each other). I guess that would satisfy 90+% of the needs for a c( ) function in an Rcpp setting...  

Best regards

-----Original Message-----
From: Kevin Ushey [mailto:kevinushey at gmail.com] 
Sent: 22. januar 2014 01:10
To: Søren Højsgaard
Cc: rcpp-devel at lists.r-forge.r-project.org (rcpp-devel at r-forge.wu-wien.ac.at)
Subject: Re: [Rcpp-devel] Rcpp version of c( )

Hi Søren,

I like the idea. Currently there is nothing like that in Rcpp. It could be made more flexible if we:

1. Accept a generic set of vectors that could be appropriately casted as needed, 2. Cast these vectors to the appropriate type if necessary, 3. Fill an output vector with the elements of these vectors.

Of course, we would like to be able to accept primitives as well so that e.g. c(x, 1, y) works so something a bit more designed would be nice.

There should be a flexible way to implement this using variadic templates, so that e.g.

template <typename T, typename... Args>
T c( Args... args ) {
    ...do the concatenation...

Of course, this requires C++11 so it might not fit until R 3.1.0 is out with its C++11 support, but it could be a natural fit in Rcpp11.
Adding the generic version in Rcpp would require code bloat unfortunately.


On Tue, Jan 21, 2014 at 2:02 PM, Søren Højsgaard <sorenh at math.aau.dk> wrote:
> Dear all,
> I have made the following primitive "concatenate" function, because I couldn't find one in Rcpp:
> template <const int RTYPE>
> Vector<RTYPE> do_conc_(Vector<RTYPE> x, Vector<RTYPE> y){
>   int nx=x.size(), n=x.size()+y.size(),i,j;
>   Vector<RTYPE> out=no_init(n);
>   for (i=0; i<nx; ++i){ out[ i ] = x[ i ];}
>   for (j=i, i=0; j<n; ++j, ++i){ out[ j ] = y[i] ;}
>   return out;
> }
> // [[Rcpp::export]]
> SEXP conc( SEXP& XX_, SEXP& YY_){
>   int type = TYPEOF(XX_) ;
>   switch( type ){
>   case INTSXP  : return do_conc_<INTSXP> ( XX_, YY_ ) ;
>   case REALSXP : return do_conc_<REALSXP>( XX_, YY_ ) ;
>   case STRSXP  : return do_conc_<STRSXP> ( XX_, YY_ ) ;
>   case VECSXP  : return do_conc_<VECSXP> ( XX_, YY_ ) ;
>   }
>   return R_NilValue ;
> }
> As you can see it assumes that the two inputs XX_ and YY_ are of the same type, and it fails to copy names to the output. If I have missed any such functionality in Rcpp then I would be happy to know...
> Cheers
> Søren
> _______________________________________________
> Rcpp-devel mailing list
> Rcpp-devel at lists.r-forge.r-project.org
> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-deve
> l

More information about the Rcpp-devel mailing list