From murdoch at stats.uwo.ca  Mon Jul  4 19:37:36 2005
From: murdoch at stats.uwo.ca (Duncan Murdoch)
Date: Mon, 04 Jul 2005 13:37:36 -0400
Subject: [Rd] How difficult is it to wrap a large C++ library with R?
In-Reply-To: <6ea7b5430507041001273ae498@mail.gmail.com>
References: <6ea7b5430507041001273ae498@mail.gmail.com>
Message-ID: <42C973E0.1020206@stats.uwo.ca>

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 R

That'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



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