2

For performance reasons, I'm working on implementing one of the functions in my R package at the C level using R's .Call. interface. I'd like my C function to be able to use some of the already existing C functions defined in another package (network) to access and manipulate data structures. However, I'm a C newbie, and I have not been able to figure out how to correctly set up / modify the C function registration in network and the import definitions in my package so that I can directly call the C code without creating a duplicate copy of the code in my package.

The CRAN documentation https://cran.r-project.org/doc/manuals/r-release/R-exts.html#Linking-to-native-routines-in-other-packages seems to say that cross-package linking is possible, and that the lme4 and Matrix packages provide an example. (I can find the R_RegisterCCallable in Matrix's init.c file, but I have not been able to locate the corresponding R_GetCCallable calls in lme4.)

Perhaps the step I'm missing is the correct portable way to import the API header from network/inst/include into my package's C src?

My question is very closely related to the following three, but the solutions seem to all use Rcpp, and so seem to gloss over the import definition step (probably because it is obvious to everyone except a C newbie like me).

using C function from other package in Rcpp

How do I share C++ functions in Rcpp-based libraries between R packages?

Best way to use c++ code from R package FOO in package BAR

Community
  • 1
  • 1
skyebend
  • 1,079
  • 6
  • 19

1 Answers1

2

I have a few working and worked examples:

  1. xts importing from zoo
  2. RcppRedis importing from RApiSerialize
  3. RcppKalman (on GitHub) importing from expm

and of course the grand old (but more complicated) example of lme4 importing from Matrix. Note that none of the exporting packages uses Rcpp. And even if the importing packages do, take that as a mere distraction. This is a plain C interface so whichever package uses it is also using plain C for this.

Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418
Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
  • 1
    looking at the RcppRedis example you provided I can see that the `#include ` line gives the exact name of the file provided by RApiSerialize in its `/inst/include` directory. And no additional path information is necessary for the compiler to locate the header file – skyebend Dec 12 '15 at 01:14
  • Correct. `LinkingTo:` in `DESCRIPTION` takes care of the path. – Dirk Eddelbuettel Dec 12 '15 at 04:14