My group has been developing an R package to simulate plant growth (see GitHub repository). The R package uses .Call
to interface with C.
We have decided that it would be worthwhile to create a standalone C library. The two key reasons are 1) to use familiar C debugging tools and 2) a large portion of the developer / user community is familiar with compiled languages (mostly models in the class are written in C or Fortran). However, the R package is accessible to many outside this community, so we want to maintain its functionality.
I have reviewed some related questions, e.g. https://stackoverflow.com/q/12328156/199217, that discuss R packages with C library dependencies, but have not found one that deals specifically with decoupling an existing R package.
A proposed approach
(what we have come up with so far ... a strawman)
- Write tests for existing functionality
- keep the C library inside the
src/
folder - Place R-specific C code (e.g.
SEXP
, loading R libraries, etc) into 'R wrapper' files prepended withR_*
- create separate functions for reading configuration files in C
- create a 'main' C function to replace functionality in R
- write a makefile for the C library that ignores R wrapper files
- Once the C library works independently and equivalently to the R package, we could consider moving the C functions to a separate repository, that would be a dependency for the R package
Questions:
- Is this effort misguided?
- Are we overlooking any potential pitfalls?
- Is there a better way to develop both the R and C libraries in parallel?
- Are there any examples of C libraries that have been decoupled from R packages?
- How might we write tests to compare equivalent functions in R and C?