Building R Packages

Many R packages contain C, C++ or Fortran code that must be compiled1 for WebAssembly (Wasm) before they can be loaded in webR. While the webR binary repository provides pre-compiled Wasm binaries2, custom R packages must be independently compiled from source.

Note

It is not possible to directly install packages from source in webR. This is likely to remain the case in the future, as such a process would depend on an entire R development toolchain running in the browser. Loading pre-compiled WebAssembly binaries is the only supported way to install packages in webR.

Compiling R packages using GitHub Actions

GitHub Actions can be used to remotely build R packages for Wasm by making use of the workflows provided by r‑wasm/actions. This is a convenient method that does not require the setup of a local WebAssembly compiler toolchain or webR development environment.

The workflow can also be configured to automatically upload the resulting R package binaries to GitHub Pages, ready to be used with webR’s install() or mount() functions.

Further details can be found in the documentation and examples directory of r‑wasm/actions.

Compiling R packages locally

To locally build R packages for webR we recommend using the rwasm package in a native R installation. The package automates the use of R’s Makevars mechanism3 to cross-compile R packages for Wasm, by setting up the host environment so that the Emscripten compiler toolchain is used for compilation and applying a selection of configuration overrides for certain packages.

The rwasm package can also be used to deploy the resulting R package binaries as a CRAN-like repository, or as an Emscripten filesystem image containing an R package library. The resulting collection of R packages can then be made available through static file hosting4.

Running the rwasm package requires pre-setup of a configured webR development environment, either by running the package under a pre-prepared webR Docker container, or by configuring a local webR development build. See the Getting Started section of the rwasm package documentation for further details.

Footnotes

  1. Prerequisite system libraries must also be compiled for WebAssembly. The webR build process optionally includes building a collection of system libraries for Wasm.↩︎

  2. Other R package repositories providing WebAssembly binaries are also available, such as R-universe.↩︎

  3. While it is possible to manually build R packages using R’s Makevars mechanism alone, the process can be quite involved and error-prone depending on the specific R package involved.↩︎

  4. e.g. GitHub Pages, Netlify, AWS S3, etc.↩︎