Skip to content
Prev 47502 / 63424 Next

Linking to native routines in other packages

Hello, 

The problem is that you have logic in both your mother and child packages. IMO, you should only have logic in the mother package. 

I?ve done this in a number of packages, it requires a bit of work initially, but not that much. 

What I?d do is have something like this in mother/inst/include/mother.h : 

#if defined(COMPILING_MOTHER)
// just declare, will be defined in test.c
SEXP fun(SEXP test) ; 
#else
inline SEXP fun(SEXP test){
    typedef SEXP(*Fun)(SEXP); 
    static Fun fun = (Fun)R_GetCCallable("mother", "fun") ;
    return fun(test) ;
}
#endif

In your test.c file, make sure you define COMPILING_MOTHER before you include mother.h, something like this

#include <R.h>
#include <Rinternals.h>
#include <R_ext/Rdynload.h>
#define COMPILING_MOTHER
#include <mother.h>

SEXP fun(SEXP);

void R_init_mother(DllInfo *dll) {
    R_RegisterCCallable("mother", "fun", (DL_FUNC) &fun);
}

SEXP fun(SEXP test) {
    Rprintf("fun so much fun\n");
    return R_NilValue;
}

So that in your child package you only have to use it, something like: 

#include <Rinternals.h>
#include <R_ext/Rdynload.h>
#include <mother.h>

SEXP afun(SEXP test) {
   fun(test);
   return R_NilValue;
}

Note that if you only want the interface between the two packages to be at low level (not visible from R), then you don?t need to conform to the SEXP(SEXP...) interface. You can use anything you like. 

Romain

Le 22 janv. 2014 ? 19:56, Gregor Kastner <gregor.kastner at wu.ac.at> a ?crit :