The text below went out as a post to the r-packages list a few days ago, but I thought it would make sense to post it on the blog too. So with a little html markup...

`Rcpp::IntegerVector`

,
`Rcpp:NumericVector`

, `Rcpp::Function`

, `Rcpp::Environment`

, ...) that makes it
easier to manipulate R objects of matching types (integer vectors, functions,
environments, etc ...).
Rcpp takes advantage of C++ language features such as the explicit
constructor / destructor lifecycle of objects to manage garbage collection
automatically and transparently. We believe this is a major improvement over
use of `PROTECT`

/ `UNPROTECT`

. When an Rcpp object is created, it protects the
underlying SEXP so that the garbage collector does not attempt to reclaim the
memory. This protection is withdrawn when the object goes out of
scope. Moreover, users generally do not need to manage memory directly (via
calls to new / delete or malloc / free) as this is done by the Rcpp classes
or the corresponding STL containers.
A few key points about Rcpp:
- a rich API covering all core R data types including vectors, matrices, functions, environments, ... (with the exeception of factors which are less useful in C++)
- seamless (bi-directional) data interchange between R and C++
- possibility of inline use permitting definition, compilation, linking and loading of C++ functions directly from R
- extensive documentation now covering eight vignettes
- exception handling and error propagation back to R
- extensive test suite using RUnit covering over 700 tests
- extension packages RcppArmadillo and RcppGSL provide easy-to-use integration with the Armadillo (linear algebra) and GNU GSL librasries
- increasing adoption among R users and package developers with now twenty packages from CRAN or BioConductor depending on Rcpp
- support for the legacy 'classic' Rcpp is now provided by the RcppClassic package which is being released concurrently with Rcpp 0.9.0

which deploys the sugar 'ifelse' function modeled after the corresponding R function. Another simple example isSEXP foo( SEXP xx, SEXP yy){ NumericVector x(xx), y(yy) ; return ifelse( x < y, x*x, -(y*y) ) ; }

where use the sugar function 'sapply' to sweep a simple C++ function which operates elementwise across the supplied vector. The Rcpp-sugar vignette describes sugar in more detail.double square( double x){ return x*x ; } SEXP foo( SEXP xx ){ NumericVector x(xx) ; return sapply( x, square ) ; }

which (after compiling and loading) we can access in R asconst char* hello( const std::string& who ){ std::string result( "hello " ) ; result += who ; return result.c_str() ; } RCPP_MODULE(yada){ using namespace Rcpp ; function( "hello", &hello ) ; }

In a similar way, C++ classes can be exposed very easily. Rcpp modules are also described in more detail in their own vignette.yada <- Module( "yada" ) yada$hello( "world" )

The RcppGSL package permits easy use of the GNU Scientific Library (GSL), a collection of numerical routines for scientifc computing. It is particularly useful for C and C++ programs as it provides a standard C interface to a wide range of mathematical routines such as special functions, permutations, combinations, fast fourier transforms, eigensystems, random numbers, quadrature, random distributions, quasi-random sequences, Monte Carlo integration, N-tuples, differential equations, simulated annealing, numerical differentiation, interpolation, series acceleration, Chebyshev approximations, root-finding, discrete Hankel transforms physical constants, basis splines and wavelets. There are over 1000 functions in total with an extensive test suite. The RcppGSL package provides an easy-to-use interface between GSL data structures and R using concepts from Rcpp. The RcppGSL package also contains a vignette with more documentation.

Dirk Eddelbuettel, Romain Francois, Doug Bates and John Chambers

December 2010