Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Building an R package which uses a c code library

Tags:

c

r

I want to submit a package, but when reading to R repository policies, I found that: "Source packages may not contain any form of binary executable code.". I currently have a binary executable for mac, win and Linux in .../inst, and the package works very fine in all os. However, I think that CRAN won't accept this. I have begun to find some info, but I am new to C and all I read is very confusing. It seems that there are several ways to carry out this and I don't know which one is the best.

My binary executable needs a specific class file in the directory, which is rendered by a function.

All my functions depend on the c library. So, what is the action I should do to get the acceptance of CRAN? ...e.g, compile with Rtools and put the executables in .../inst? Leave the library in .../inst and make a function that will do the work?

Self-study. Updates about the steps taken to solve the problem:

Install Rcpp package,

install.packages("Rcpp")

and install R tools. Create a new package in R studio of type "Package w/ Rcpp" and select the main file of my c code library in "Create package based on source files".

like image 611
fina Avatar asked Dec 23 '22 21:12

fina


2 Answers

Your analysis is correct: a binary won't fly at CRAN. So you need to make it a library. Or, rather, a package for CRAN.

That is not as hard as it sounds. First off, if you wrote the binary you probably know how to reorganize the code to do

void main() {
     // preliminary stuff on argc/argv
     // sort our arguments
     // call main function f(a, b, c)
}

If you can get to that spot, you're almost done. Place the file doing f(a,b,c) into src/, and use (fairly simple, comparatively speaking) Rcpp code tp pass a, b and c to your function -- and to then get the results (a numeric vector, maybe?) back.

Start with the Rcpp-introduction vignette and take it from there.

like image 118
Dirk Eddelbuettel Avatar answered Dec 25 '22 10:12

Dirk Eddelbuettel


A single SO answer is not enough to explain all the details of interfacing R with C/C++. Just getting C/C++ code to compile on different systems is a deep rabbit hole, and that's what's going to be the tricky part in problems like yours.

CRAN expects all packages that need an executable to include the source code for it, as well as whatever is necessary to configure its compilation. The former entails that whatever library you depend on must have open source code, and a license that's compatible with CRAN policies; the latter entails making sure the library can be built by R's toolchain.

Take for instance RcppParallel, which essentially wraps Intel's TBB for R packages to use. It includes the latter's source code under src, and mentions relevant copyrights and system requirements on its description (as seen on its CRAN page). From what I can tell, the developers had to write a Makevars.in file to integrate it with R.

That is just one example, and unfortunately there's no one answer that fits all cases. Look for example at the README for httpuv (scroll down there).

I don't mean to be discouraging, but integrating existing C/C++ libraries with R is indeed complex. If you want to do it, you'll have to be patient and start with the basics. If the library you need isn't open source, I doubt you can get your package on CRAN. If it is, check how it is compiled as stand-alone, and go from there.

Oh and BTW, Rcpp is not integrating existing libraries with R, it is essentially a package with C++ interfaces for R's C API (and a bunch of extra functionality on top of that), so it's not the best example for what you're trying to do.

like image 32
Alexis Avatar answered Dec 25 '22 09:12

Alexis