简体   繁体   中英

What is the correct way to import C header functions into an R package when they are exported from another?

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM