Rcpp 0.8.3

June 29, 2010
By

(This article was first published on dirk.eddelbuettel, and kindly contributed to R-bloggers)

A new version 0.8.3 of Rcpp is now CRAN and in Debian.

It comes about three weeks after the 0.8.2 release. And even though we promised to concentrate on documentation, it contains a raft of new features:

  • The addition of what we dub Rcpp sugar: some syntactic sugar based on clever use of expression templates that lets us write C++ expression as neatly and compactly as vectorised R expressions (while getting C++ speed!!). More on that below.
  • New classes Date and Datetime with internal representations just like R's Date and POSIXct classes, plus vector versions DateVector and DatetimeVector which behave just like STL vectors. With that, the new API is now feature-complete compared to the older 'classic' API.
  • Rcpp Modules (our 'not unlike Boost::Python' feature introduced in version 0.8.1) can now expose public data members
  • A new API class InternalFunction which can expose C++ functions even without R modules.

The main thing here is Rcpp sugar for which we also have a new (seventh !!) vignette Rcpp-sugar. As a quick example, consider this simple C++ function that takes two vectors from R and creates a new one conditional on the relative values:

export "C" SEXP foo( SEXP xs, SEXP ys) { Rcpp::NumericVector x(xs), y(ys); int n = x.size(); Rcpp::NumericVector res( n ); double xd = 0.0, yd = 0.0 ; for( int i=0; i<n; i++){ xd = x[i]; yd = y[i]; if( xd < yd ){ res[i] = xd * xd ; } else { res[i] = -( yd * yd); } } return res ;
}
Now, if you use R, you really want to writes this more compactly. And now you can, thanks to Rcpp sugar:
extern "C" SEXP foo( SEXP xs, SEXP ys){ Rcpp::NumericVector x(xs), y(xs); return ifelse( x < y, x*x, -(y*y));
}
Same great taste, but much less filling! More details are in the Rcpp-sugar vignette. Doug Bates is already a fan of this and is employing it in the lme4a development version of the well-known lme4 package.

The full NEWS entry for this release follows below:

0.8.3 2010-06-27 o	This release adds Rcpp sugar which brings (a subset of) the R syntax into C++. This supports : - binary operators : <,>,<=,>=,==,!= between R vectors - arithmetic operators: +,-,*,/ between compatible R vectors - several functions that are similar to the R function of the same name: abs, all, any, ceiling, diff, exp, ifelse, is_na, lapply, pmin, pmax, pow, sapply, seq_along, seq_len, sign Simple examples : // two numeric vector of the same size NumericVector x ; NumericVector y ; NumericVector res = ifelse( x < y, x*x, -(y*y) ) ; // sapply'ing a C++ function double square( double x ){ return x*x ; } NumericVector res = sapply( x, square ) ; Rcpp sugar uses the technique of expression templates, pioneered by the Blitz++ library and used in many libraries (Boost::uBlas, Armadillo). Expression templates allow lazy evaluation of expressions, which coupled with inlining generates very efficient code, very closely approaching the performance of hand written loop code, and often much more efficient than the equivalent (vectorized) R code. Rcpp sugar is curently limited to vectors, future releases will include support for matrices with sugar functions such as outer, etc ... Rcpp sugar is documented in the Rcpp-sugar vignette, which contains implementation details. o New helper function so that "Rcpp?something" brings up Rcpp help o Rcpp Modules can now expose public data members o New classes Date, Datetime, DateVector and DatetimeVector with proper 'new' API integration such as as(), wrap(), iterators, ... o The so-called classic API headers have been moved to a subdirectory classic/ This should not affect client-code as only Rcpp.h was ever included. o RcppDate now has a constructor from SEXP as well o RcppDateVector and RcppDatetimeVector get constructors from int and both const / non-const operator(int i) functions o New API class Rcpp::InternalFunction that can expose C++ functions to R without modules. The function is exposed as an S4 object of class C++Function 

As always, even fuller details are in Rcpp Changelog page and the Rcpp page which also leads to the downloads, the browseable doxygen docs and zip files of doxygen output for the standard formats. A local directory has source and documentation too. Questions, comments etc should go to the rcpp-devel mailing list off the R-Forge page

To leave a comment for the author, please follow the link and comment on his blog: dirk.eddelbuettel.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.