Re: [Rd] How difficult is it to wrap a large C++ library with R?
> There was a couple of posts about this recently: > > https://stat.ethz.ch/pipermail/r-devel/2005-April/subject.html > I am too new to this list to see these discussions. They are quite interesting! Certainly I was not aware of the big differences between C++/Python OOP and S3/S4 OOP. This explains why I skipped the OOP part of the red S-Plus manual when I was learning S-Plus: I was not able to get the ideas although I was an experienced C++ programmer. Regardless of the question of 'which OOP is better', I certainly agree with Nathan's opinion that R has lost many users/developers who only know C++/Python OOP. I think I would better continue to use Python to wrap my C++ code (until Ali find a good way to automatically wrap C++ code, or SWIG adds R support). It is simply much easier and more efficient. Destructing my C++ classes into C pieces and reconstruct them in S4 way would be tedious, and the performance penalty will most possibly be disastrous for this simulation environment. Cheers, Bo __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [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 __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [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 __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] How difficult is it to wrap a large C++ library with R?
On 7/4/05, Bo Peng <[EMAIL PROTECTED]> 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"). __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] How difficult is it to wrap a large C++ library with R?
On 7/4/05, Bo Peng <[EMAIL PROTECTED]> 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)! What makes you think that there is no inheritance of classes in R? > > > * 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(). Well the R object model is based on generic functions and methods are functions. The S4 class structure differs from the S3 structure in that there is a formal definition of an S4 class but not for an S3 class. However, even in S4, methods do not belong to a class. Methods are intermediate to classes and generic functions and are associated with a signature for the arguments of the generic function. If you want to be able to use S4 classes effectively you should use the .Call interface to compiled code and not the .C interface. If you pass a classed object through the .Call interface to compiled code you can extract or set the values of slots in the object through the GET_SLOT and SET_SLOT macros. Admittedly this is not as convenient for the C++ programmer as being able to pass a pointer to an object from a C++ class but it does make it easier to think of objects in R and the corresponding objects in compiled code. The Matrix package on CRAN contains many examples of this use of this technique. Another technique that you may want to consider is the use of ExternalPointers. If you examine the C source file lmer.c from the aforementioned Matrix package you will see where I create a pointer to a C struct in the compiled code and pass it back to R so it can later be passed to the C code and I can extract the struct. The same could be done for an instance of a C++ class. __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] How difficult is it to wrap a large C++ library with 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). 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(). Thanks. Bo __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [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 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@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel __ R-devel@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?
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/ __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel