A simple example repo to test making an R package from C++ code using SWIG to generate wrapper code. The goal is to make it easy to install for both Linux, MacOS and Windows users. Windows users should be able to install a binary package to not require tools to compile the C++ code.
Swigr can be installed as a source package from R by running
devtools::install_github("danieledler/swigr")Binary bundle can be installed from R by running
install.packages("https://github.com/danieledler/swigr/releases/download/v0.0.2/swigr_0.0.2.tgz")Then test with
swigr::test_example()To build the R wrapper, run make build. To build a binary R package after that, run make build-binary. This creates a .tgz file on Mac, which is possible to install using install.packages(file_path_or_url).
R packages can be in five different states:
- source
- bundled
- binary
- installed
- in-memory
devtools::build() creates a bundled package and can be installed with devtools::install_github("danieledler/swigr"). However, the C++ source code is included in a src directory and will be compiled on the user's computer, which creates problems for many Windows users.
devtools::build(binary = T) creates a binary package that instead of a src folder has a libs folder with the C++ code compiled to a shared library. This is required for devtools::install_url("...") to work, but install_url also requires a man folder which is not present when building a binary package.
CRAN makes binaries available from bundles and includes a man folder.
Instead, use install.packages(url), which works both for local file paths and remote urls.
Package Development:: CHEAT SHEET
- Add
zzz.Rwith.onUnload()to clean SWIG? - Use Github Actions to build R package