Using Quarto and WebAssembly
May 1, 2024
For those new to Revealjs, change slides using…
j
, or right arrowk
or left arrowAdditional options:
m
o
alt
(windows) or option
(mac)These slides were made using Quarto’s Revealjs format under the {quarto-stanford}
theme.
See source of the presentation on GitHub at:
https://github.com/coatless-talks/stats352-guest-lectures-on-dynamic-interactions-wasm
.wat
file for WebAssembly.main.wat
(module
;; Allocate a page of linear memory (64kb). Export it as "memory"
(memory (export "memory") 1)
;; Write the string at the start of the linear memory.
(data (i32.const 0) "Hello, world!") ;; write string at location 0
;; Export the position and length of the string.
(global (export "length") i32 (i32.const 12))
(global (export "position") i32 (i32.const 0)))
Convert to binary with:
Access with JavaScript:
No worries! We can use Emscripten:
Note
There are more languages available for WASM compilation.
Let’s take for example the hello_world.c program
We can convert it using Emscripten to a webpage with:
Previewing the generated webpage, e.g. hello_world.html, requires a local server to see output.
There’s a bit more nuance since R and Python extensively use Fortran in:
This brings in the need to use llvm-flang
. For more, see George Stagg’s Fortran on WebAssembly post.
🔗 https://ktock.github.io/container2wasm-demo/
(Warning: Minimum 200MB download!)
Command+Option+J
Control+Shift+J
latest
tag, we’re using the development version.latest
to a specific version, e.g. v0.3.3
, to pin the evaluation.Let’s try evaluating some R code using our webR instance.
In console, type:
await
.01:30
Open up your copy of R, what values are generated when running:
Does it match with the webR output?
A server is a type of computer that is operating 24/7 on the internet that is interacting with your own computer.
We can think of servers in two ways:
Note
There are more types of servers available; but, our discussion rests solely on those two.
Note
We can substitute the R logo with Python’s in these diagrams.
{fig-alt=“An static image showing the source of a hidden solution on a page.” fig-align=“center”“}
{r}
-> {webr-r}
or {python}
-> {pyodide-python}
.🔗 https://repo.r-wasm.org (Warning: Minimum 75 MB)
r-universe.dev offers binaries based on an R package repository’s most recent commit:
Outside of the Python packages built-in to Pyodide, the number of Python packages varies as there is no central repository.
*py3-none-any.whl
), then the package can be used as-is.
basic-colormath
on PyPI*-cp310-cp310-emscripten_3_1_27_wasm32.whl
quarto-stanford
), Filters (quarto-{webr,pyodide}
), Shortcodes (quarto-embedio
), Revealjs Plugins, Journals (quarto-jss
).If you are comfortable with VS Code, you can jump right into an authoring Codespace by clicking on the following button:
Note: Codespaces are available to Students and Teachers for free up to 180 core hours per month through GitHub Education. Otherwise, you will have up to 60 core hours and 15 GB free per month.
Open or Create an RStudio Quarto Project
Navigate to the Terminal tab in lower left side of RStudio
Type the install command:
quarto add coatless/quarto-webr
and press enter.
Voila! It’s installed.
The project directory should contain the following structure:
.
├── _extensions
│ └── coatless/quarto-webr # Added by 'quarto add'
├── _quarto.yml # Created by 'quarto create'
└── webr-demo.qmd # Quarto Document with webR
Important
If the _extensions
directory is not found within a Quarto project, the project is not using any extensions!
engine: knitr
webr
Filter{webr-r}
instead of {r}
Cmd (⌘) + Shift (⇧) + K
Ctrl + Shift + K
Or, you can press the “Render” button
First, install the {quarto-pyodide} extension using Terminal with:
Next, register the extension in the Quarto Document with:
Finally, use {pyodide-python}
instead of {python}
when creating a code cell.
There are two types of options in {quarto-webr}:
Note
The cell-level options use a custom code cell parser called {quarto-codecelloptions} and, thus, are not exactly 1-to-1 with Quarto options.
For example, we could disable the status indicator and pre-load different R packages by specifying in the document’s YAML header the webr
meta key:
Cell-level options direct the execution and output of executable code blocks. These options are specified within comments at the top of a code block by using a hashpipe, e.g. #| option: value
.
context
cell option handles how the code is executed and displayed to a user.interactive
, which gives us a runnable code cell.context
are:
context: output
which only shows the outputcontext: setup
which shows neither output nor code.Important
{quarto-pyodide} has yet to receive the options treatment! We only make available an interactive editor.
To make your Quarto document accessible on GitHub Pages via Quarto, use the following command in Terminal:
This option is great if you want to share your document through a GitHub Pages website.
Alternatively, you can publish your Quarto document on Quarto Pub via Quarto. Use the following command in Terminal:
This option provides you with a shareable link for easy access by others and is a good choice if you prefer a dedicated platform for your documents.
Continuous deployment (CD) is the notion that each time a collaborator contributes code into a branch of the repository, the code is automatically built and put into a production environment.
Let’s say we want to use a GitHub action for building a webR package alongside a pkgdown website using GitHub, inside of our RStudio package project we would run:
# install.packages("usethis")
usethis::use_github_action(
"https://raw.githubusercontent.com/r-wasm/actions/main/examples/rwasm-binary-and-pkgdown-site.yml"
)
This copies the YAML file for the action and sets it up inside of the .github/workflows
folder, e.g.
Next, we’ll specify what operating system will be used, restrict multiple jobs from running, and describe the permissions the workflow has.
# ... previous slide
steps:
- name: "Check out repository"
uses: actions/checkout@v4
- name: "Setup pandoc"
uses: r-lib/actions/setup-pandoc@v2
- name: "Setup R"
uses: r-lib/actions/setup-r@v2
- name: "Setup R dependencies for Quarto's knitr engine"
uses: r-lib/actions/setup-r-dependencies@v2
with:
packages:
any::knitr
any::rmarkdown
any::downlit
any::xml2
Next, we obtain a copy of the repository.
Then, we specify the different software dependencies for the project.
As we’re using the engine: knitr
, this requires additional R package dependencies.
In the next part of the file, we focus on Quarto:
coatless/quarto-webr@v0.4.1
.qmd
files in the current directoryFinally, we take our rendered Quarto documents and create a zip archive that can be used on GitHub Pages.
Then, we deploy that archive onto GitHub pages.
From here, our augmented Quarto documents with interactivity should be available for everyone!
https://quarto.thecoatlessprofessor.com/quarto-webr-pyodide-demo/
You can see the deployment repository here:
Thank you for the invitation to talk today!
Questions