[Rd] How difficult is it to wrap a large C++ library with R?

Bo Peng ben.bob at gmail.com
Mon Jul 4 19:01:05 CEST 2005

Dear list,

I have developed a forward-time population genetics simulation
environment simuPOP, which is a set of C++ (template)
classes/functions wrapped by SWIG as Python libraries. R is used
extensively as plotting and statistical analysis engine through RPy

I use Python to wrap simuPOP since most the following can be easily
done using SWIG or Python C API. However, since Python is less used by
bioinformaticists and geneticists, I am asked again and again why I do
not wrap simuPOP with R (R is also OOP etc...). Although I am a long
time R user, I am not familiar with package writing in R. From what I
read from R website, it is easy to wrap individual C/C++ functions but
not C++ classes. Can anyone take the time to review the following and
tell me if they can be done (easily or need effort) using R? If most
of the answers are yes, it may be a good idea to switch to R.

* Wrap C++ class hierarchy. Virtual functions need to be supported.
(SWIG can generate Python shadow classes that behave almost exactly
like the underlying C++ classes)

* Be able to do this:
    evolve(ops=c(obj1, obj2, obj3))
  Internally, evolve will call virtual function fun() of obj1, obj2, .etc.
  obj1, obj2, ... are objects derived from the same base class. 

* Direct access to C++ data structure. For example, an object may keep
a C array internally. I will need a method (maybe wrap this data into
a R object) to read/write this array directly.

* create and read/write a R list at C++ level. (Need API for R/list read/write)

* pass a user defined R function to C++ function. Call it and obtain result.

* evaluate an R expression (passed as string) from within C++ function
and obtain result.

* pass C++ exceptions to R

* be able to use C++ features like template, STL.

* organize manual by objects, not functions. (I notice that manuals of
R libraries are simple function references.)

Many thanks in advance.

Bo Peng
Department of Statistics
Rice University

More information about the R-devel mailing list