Loading and Using Packages
Integrating R and Python packages into interactive Quarto documents
Both webR and Pyodide supports downloading and using additional extension packages at runtime. However, outside of its dependencies, quarto-live avoids automatically downloading additional packages. There are two supported ways to download packages:
- Installing packages as the document loads.
- Installing packages using interactive code blocks.
Installing packages
As the document loads
To install packages as part of the document WebAssembly startup process, add a packages
key to your document’s YAML header, under the key corresponding to your chosen WebAssembly engine.
example-r.qmd
---
format: live-html
webr:
packages:
- dplyr
- palmerpenguins
- ggplot2
---
example-py.qmd
---
format: live-html
pyodide:
packages:
- matplotlib
- numpy
- seaborn
---
Custom repositories
Many R and Python packages that are not available from the default webR and Pyodide repositories can still be used with quarto-live
, with a little extra setup work.
Custom R packages can be compiled for WebAssembly using the {rwasm} R package. Once an R package has been compiled for Wasm, it should be hosted in the form of a CRAN-like repository.
The simplest way to do this to make use of the R-universe package repository service. R-universe will build WebAssembly versions of your R packages automatically for your own personal CRAN-like repository. This allows you to host custom R package binaries not just for webR, but also for the macOS, Linux, and Windows versions of R.
Whichever method you choose, once you have a CRAN-like repository that contains WebAssembly binaries make a note of its URL. It should be included as part of a repos
array, under the webr
key, in your document’s YAML header.
example-r.qmd
---
format: live-html
webr:
packages:
- cli
repos:
- https://r-lib.r-universe.dev
---
If a package is not found in the Pyodide repository it will be loaded from PyPI. The micropip
package can load PyPI packages for Pyodide if they are built as pure Python wheels. Python packages containing compiled code should be built as a wasm32/emscripten
WebAssembly wheel, following build instructions provided by the Pyodide team.
If you don’t want to host custom Python packages on PyPi, micropip
can also install wheels directly from URL. Host your Python package wheel on a static web hosting service, such as GitHub Pages. Then, include the full URL to your package in the packages
key in your document’s YAML header.
example-py.qmd
---
format: live-html
pyodide:
packages:
- https://username.github.io/mypackage/mypackage-0.0-1-py3-none-any.whl
---
Interactively
Install R packages interactively using the standard install.packages()
R function.
WebR is configured to download R packages from the webR public package repository, and the repository website lists all R packages provided for WebAssembly.
By default, webR outputs information about each package installed in this way. These messages can be suppressed using the quiet = TRUE
named argument.
Pyodide ships with the micropip package, which can be used to install WebAssembly compatible Python packages provided by the Pyodide team using the micropip.install()
function.
If a package is not found in the Pyodide repository it will be loaded from PyPI. Note that micropip
can only load pure Python wheels or wasm32/emscripten
WebAssembly wheels.
The micropip.install()
function is asynchronous. It must be awaited before the package can be loaded.