[Rd] Interfacing a C++ class

Romain Francois romain at r-enthusiasts.com
Mon Jun 6 13:42:48 CEST 2011

Le 04/06/11 16:31, soeren.vogel at uzh.ch a écrit :
> Hello
> Apologies for cross-posting, the discussion should (if) go to R-devel, but I also want to reach the rcpp-devel people.
> My C++ class FOO is a module available through Rcpp, and it works fine and is -- so far -- bug free. With trying to further develop my R package, I thought it was a good idea to interface my C++ workhorse FOO with an S4 class Foo. After some long and not always insightful trip through S4 classes and methods, I am not sure if I am on the right way of thinking and designing. Since there is no local assistance available, could you help to make things clearer?
> Just a brief outline:
> FOO is the C++ object, and Foo should be the S4 class. If the user creates an object, say bar, from class Foo, the creation process automatically makes a new FOO object relating to bar in that a unique name of the FOO instance is stored in a slot of bar. All the user then has to do is modify bar by simple assignments. The getters and setters ("$", "[") are set up and work. Each modification goes in hand with assigning new values to bar as well as updating the FOO object through available setters from FOO.
> So far, this way has brought me to about 100 lines, but now I read about ReferenceClasses and was wondering, if there is a much easier way of achieving my goals. Moreover, I was not sure any longer if my goals make sense or if a more advanced programmer would do it totally different (and could share some structural thinking on their approach).
> The idea behind my way of doing was based upon several considerations. First, a "classical" R object would not confuse users, as I assume users of my package to be rather non-skilled (and not willing to change that). Second, I want to create a properly programmed package for distribution on CRAN and publication, eventually. Third, I want to save objects across sessions. So if a user restores a session, a simple command, say, restore(), would do the trick to build all the related C++ objects again. However, I admit that I still have not figured out how to automatically clean up the workspace correctly before leaving a session, wanted or unwanted, that is, clean up memory before leaving home. Fourth, pure arithmetic check is done in C++, however, semantic check *should* be left to R, and the validity and class routines seem to be perfect for this. Fifth, some work should be done in R, such as the passing of data frames or samples from distributions.
> I hope to get some structured ideas and hints how to start and/or proceed.
> Thank you in advance,
> Sören


A C++ class that is exposed through an Rcpp module is already a 
reference class, and so you can add methods, etc ...

Consider this example :


fx <- cxxfunction( , '', includes = '

class FOO{
     FOO( double x_, double y_): x(x_), y(y_){}

     double x ;
     double y ;

     void move( double dx, double dy){
         x += dx ;
         y += dy ;
} ;


     class_<FOO>("FOO" )


         .field( "x", &FOO::x )
         .field( "y", &FOO::y )

         .method( "move", &FOO::move )

', plugin = "Rcpp" )
mod <- Module( "mod", getDynLib(fx),mustStart = TRUE )

# grab the exposed C++ class
FOO <- mod$FOO

# add R methods
     bla = function() x+y,
     reset = function() {
         x <<- 0.0
         y <<- 0.0
# create an instance
f <- new( FOO, 2.0, 3.0 )

# call an R method

# call a C++ method
f$move( 2.0, 2.0 )

# call an R method

Hope this helps.


Romain Francois
Professional R Enthusiast
+33(0) 6 28 91 30 30
|- http://bit.ly/hdKhCy : Rcpp article in JSS
|- http://bit.ly/elZJRJ : Montpellier Comedie Club - Avril 2011
`- http://bit.ly/fhqbRC : Rcpp workshop in Chicago on April 28th

More information about the R-devel mailing list