[Rcpp-devel] R classes extending C++ classes

John Chambers jmc at stat.stanford.edu
Fri Apr 20 18:46:57 CEST 2012

As of a recent version of Rcpp on r-forge (rev. 3582 or later), there is 
a new facility for defining  an R class extending a C++ class extracted 
from a module.

The main tool is setRcppClass in package Rcpp.  See its documentation.

Also in the unittests directory of package Rcpp is an example package, 
testRcppClass, that has some typical applications.

The general idea is that setRcppClass() defines a reference class built 
around a particular C++ class, with optionally additional fields and 
methods defined in R.

The R methods can refer to C++ fields and methods.

The general goal is to be able to use R for prototyping new features 
that could then be implemented in C++ if they are useful and need 

The new code relies on load-time actions, introduced in R 2.15.0, 
because information about the C++ class is not available until the 
compiled code is linked with the R process.  Load time actions allow a 
package to include setRcppClass() calls in its source code in the same 
way other class/method definitions would be.  But the action all really 
happens at load time.  In particular, any extensions to these classes 
have to be done at load time as well, using load actions or 
setRcppClass() calls.

This is all new stuff and still experimental.  There are a couple of 
known limitations, plus probably unknown glitches.  For now, objects 
from modules need to be explicitly exported if your package wants to 
export them.  (Classes are exported either explicitly or by pattern).

Also, when running your package through CMD check, you may get bizarre 
complaints about loading the package with only base attached.  And you 
will very likely get notes about code for any reference class methods 
that refer to field names, which codetools can't distinguish from global 


More information about the Rcpp-devel mailing list