I've just started working on the new package CPP, as usual the project is maintained in r-forge. The package aims at exposing C++ classes at the R level, starting from classes from the c++ standard template library.
key to the package is the CPP function (much inspired from the J function of rJava). The CPP function builds an S4 object of class "C++Class". The "C++Class" currently is a placeholder wrapping the C++ class name, and defines the new method (again this trick or making
new S4 generic comes from rJava). For example to create an R object that wraps up a std::vector<int>, one would go like this:
x <- new( CPP( "vector<int>" ) )
This is no magic and don't expect to be able to send anything to CPP (C++ does not have reflection capabilities), currently only these classes are defined : std::vector<int>, vector<double>, vector<raw> and set<int>
Because C++ does not offer reflection capabilities, we have to do something else to be able to invoke methods on the wrapped objects. Currently the approach that the package follows is a naming convention. The
$ method create the name of the C routine it wants to call based on the C++ class the object is wrapping, the name of the method, and the types of the input parameters. So for example calling the
size method for a
vector<:int> object yields this routine name: "vector_int____size", calling the
push_back method of the
vector<double> class, passing an integer vector as the first parameter yields this signature : "vector_double____push_back___integer" .... (the CPP:::getRoutineSignature implements the convention)
Here is a full example using the
set<int> class. Sets are a good example of a data structure that is not available in R. Basically it keeps its objects sorted
> # create the object > x <- new( CPP("set<int>") ) > # insert data using the insert method > # see : insert > x$insert( sample( 1:20 ) ) > # ask for the size of the set > x$size()  20 > # bring it back as an R classic integer vector > as.vector( x )  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
Currently the package is my excuse to learn about the standard template library, and it is quite possible that the functionality will be merged into the Rcpp it currently depends on. Because of this volatility, I'll use the Rcpp-devel mailing list instead of creating a new one.