Skip to content

Interfacing pre-existing C++ library from R

2 messages · Alexander Duering, Brian Ripley

#
Hello,

I'm trying to link a large in-house C++ library into R. So far, I've managed to to most of what I want, but I have one remaining question and a suggestion.

The suggestion first: I've found it useful to put

     #ifdef __cplusplus
     extern "C" {
     #endif

and
     #ifdef __cplusplus
     }
     #endif

around the function prototypes in Rinternals.h (in /usr/local/lib/R/include on my Red Hat 7.2 box) so my dynamic linker would no longer complain about mangling-related undefined symbols. Also, I had to use #define USE_RINTERNALS before including them in order to get a few macros I suppose are natural to use. To be honest, I do think the include files could do with a bit of cleaning up, in particular in view of linking in C++ code, but no complaints (I'm very happy to have R as it is).

My question is this: Our library makes extensive use of matrices of mixed type, i.e., I would need to pass in and out objects that are logically of rectangular structure, but some cells contain numbers, others strings. I had initially hoped to be able to use data frames for that, but I realise that columns have to be of identical mode. I could, if nothing else works, use string matrices, but that would be very very unsatisfactory (and take ages, because I'd have to check, and possibly convert, each matrix element). Could somebody please suggest the 'natural' R data structure for this kind of object? I'm using cell arrays in Matlab and lists of lists in Perl for that purpose, if it's any help.

Many thanks,

Alex



--

Diese E-Mail enth?lt vertrauliche und/oder rechtlich gesch?tzte Informationen. Wenn Sie nicht der richtige Adressat sind oder diese E-Mail irrt?mlich erhalten haben, informieren Sie bitte sofort den Absender und vernichten Sie diese Mail. Das unerlaubte Kopieren sowie die unbefugte Weitergabe dieser Mail ist nicht gestattet.

This e-mail may contain confidential and/or privileged information. If you are not the intended recipient (or have received this e-mail in error) please notify the sender immediately and destroy this e-mail. Any unauthorized copying, disclosure or distribution of the material in this e-mail is strictly forbidden.


-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
On Mon, 25 Feb 2002, Alexander Duering wrote:

            
on my Red Hat 7.2 box) so my dynamic linker would no longer complain about
mangling-related undefined symbols. Also, I had to use #define USE_RINTERNALS
before including them in order to get a few macros I suppose are natural to use.
To be honest, I do think the include files could do with a bit of cleaning up,
in particular in view of linking in C++ code, but no complaints (I'm very happy
to have R as it is).

Well, prior to R 1.4.1 Rinternals.h would not work with many C++ compilers,
and some changes were made because a package writer had assumed it would
(as it did with his version of g++, although not with mine nor with
Solaris' C++).  Those blocks would indicate that we believed that
Rinternals.h was safe to use in C++ code, and at the last minor release
(1.4.0) it wasn't.  It's under consideration for 1.5.0 if we can be sure
that it is now fully C++ compliant.  The `Writing R Extensions' manual is
careful not to include Rinternals.h in the description of what headers can
be used with C++. One issue is that these headers get changed frequently,
and we need to be able to verify such claims automatically.

There is nothing to stop you making your own header which includes
Rinternals.h between those blocks of code, of course.
type, i.e., I would need to pass in and out objects that are logically of
rectangular structure, but some cells contain numbers, others strings.
I had initially hoped to be able to use data frames for that, but I realise
that columns have to be of identical mode. I could, if nothing else works,
use string matrices, but that would be very very unsatisfactory (and take
ages, because I'd have to check, and possibly convert, each matrix element).
Could somebody please suggest the 'natural' R data structure for this kind of
object? I'm using cell arrays in Matlab and lists of lists in Perl for that
purpose, if it's any help.

A matrix list?  R lists are just vectors with elements of different types,
and R matrices are just vectors with a dimension attribute.