Skip to content

Example R package for C++ code with R bindings generated by SWIG

Notifications You must be signed in to change notification settings

danieledler/swigr

Repository files navigation

SwigR - An example R package using SWIG to wrap C++

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.

Install

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()

Build

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).

Issues

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.

Cheat sheet

Package Development:: CHEAT SHEET

TODO