[Rd] How difficult is it to wrap a large C++ library with R?
Gabor Grothendieck
ggrothendieck at gmail.com
Tue Jul 5 03:15:22 CEST 2005
On 7/4/05, Bo Peng <ben.bob at gmail.com> wrote:
> > > * 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).
>
> I guess I will have to learn R OOP in detail first. It would be
> terrible if I will have to re-write every C++ class in R (and without
> inheritance)!
>
> > > * 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.
>
> So this has to be done through functions. In Python, I can create an
> object to wrap C array that can be accessed just like regular list. I
> guess it would be difficult to create a new data type in R.
>
> Anyway, where can I find the most complete information about
> implementation of R/OOP at C/C++ level? (Or should I write classes in
> R and call C++ functions for every member function?) I have not read
> http://cran.r-project.org/doc/manuals/R-exts.pdf in detail but my
> impression is that almost everything is done at a function (not
> object) level using .C().
>
There are two OO models that come out-of-the-box with R namely S3 and S4.
They are inspired by the Dylan OO model. Others have already mentioned
these. There are also two additional models on CRAN in the R.oo and
proto packages which implement a more conventional class mode (R.oo)
and the prototype model (proto). R.oo has a web site (try google) and
proto has a vignette, i.e. a paper accessible via library(proto);
vignette("proto").
More information about the R-devel
mailing list