Bo Peng
2005-Jul-04 17:01 UTC
[Rd] How difficult is it to wrap a large C++ library with R?
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 package. 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 http://bp6.stat.rice.edu:8080/
Duncan Murdoch
2005-Jul-04 17:37 UTC
[Rd] How difficult is it to wrap a large C++ library with R?
On 7/4/2005 1:01 PM, Bo Peng wrote:> 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 > package. > > 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)This is hard to do in R, because the R object model is quite different from that of C++ (whereas Python's is more similar).> * 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.This wouldn't be hard, assuming that the 3 objects have classes, and there's a generic function fun() which owns methods supporting those classes. (In R, objects don't have virtual functions, generic functions do.)> * 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.That's not too hard provided you use C++ code to do the actual access. That is, you write an R function that calls C++ code to do the work. It's a lot harder if you want to keep it all in R, because it doesn't understand C++ type definitions, alignment conventions, etc.> * create and read/write a R list at C++ level. (Need API for R/list read/write)That's not too hard. There are lots of examples in contributed libraries, and it's documented in the R Extensions manual.> > * pass a user defined R function to C++ function. Call it and obtain result.Ditto.> > * evaluate an R expression (passed as string) from within C++ function > and obtain result.Ditto.> > * pass C++ exceptions to RThat's hard, because R won't know what to do with them. There are ways to turn C++ exceptions into R errors, but they are not well documented, and I suspect you'll need to do most of the work (i.e. have an exception handler that calls an internal R function).> * be able to use C++ features like template, STL.In your own C++ code? If gcc supports them, it'll be easy. If you need a special compiler, it'll be harder. In R code? Forget it.> * organize manual by objects, not functions. (I notice that manuals of > R libraries are simple function references.)You can write a vignette organized any way you like. The code that shows up in the printed manuals is just a collection of Rd man pages, organized more or less alphabetically.> Many thanks in advance. > > -- > Bo Peng > Department of Statistics > Rice University > http://bp6.stat.rice.edu:8080/ > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel
Simon Urbanek
2005-Jul-05 02:48 UTC
[Rd] How difficult is it to wrap a large C++ library with R?
On Jul 4, 2005, at 1:01 PM, Bo Peng wrote:> From what I read from R website, it is easy to wrap individual C/C+ > + functions but not C++ classes.There was a couple of posts about this recently: https://stat.ethz.ch/pipermail/r-devel/2005-April/subject.html Look for the posts concerning C++, overloading methods and objects in R. If I remember correctly "Ali" had some prototype automatic wrapper for large classes (he wasn't happy about the performance, but that's another story ;) - see his 'speeding up the library' posts). Cheers, Simon
Bo Peng
2005-Jul-05 03:22 UTC
[Rd] How difficult is it to wrap a large C++ library with R?
> SWIG is a very nice idea. ...SWIG is a wonderful tool that makes wrapping C++ code super-easy. I can write pure C++ code and even test them by writing a main() function. Then, using a simple interface file, all my C++ classes becomes Python (shadow) classes like magic. It will take much more time to develop simuPOP if I had to modify interface files manually for every change to the C++ classes.> I hope to have a prototype of a general mechanism for generating > bindings to other code for R sometime over the summer. > It has been hatching for some time and it is primarily a matter of > getting the time to implement the existing ideas.Since you already know how to wrap C++ classes to R classes, maybe it is a good idea to ask SWIG developers how to use SWIG to bridge them. After all, SWIG has a mature set of tools to parse C++ code and generate interfaces to more than a dozen scripting languages. Personally, if I have to write interfaces manually for my 40+ classes and a lot more functions, I would rather devote time in adding R support for SWIG. Thanks. Bo