Giter Site home page Giter Site logo

quarto-webr's Introduction

webR Code Extension for Quarto HTML Documents

This extension enables the webR code cell in a Quarto HTML document.

quarto-webr Filter in Action

The goal of webR is to:

run R code in the browser without the need for an R server to execute the code

For more details on webR, please see:

Installation

To use this extension in a Quarto project, install it from within the project's working directory by typing into Terminal:

quarto add coatless/quarto-webr

Demonstration of using the Terminal tab to install the extension.

Usage

For each document, place the the webr filter in the document's header:

filters:
  - webr

Then, place the code for webr in a code block marked with {webr}

---
title: WebR in Quarto HTML Documents
format: html
engine: knitr
filters:
  - webr
---

This is a webr-enabled code cell in a Quarto HTML document.

```{webr}
fit = lm(mpg ~ am, data = mtcars)
summary(fit)
```

When quarto render or quarto preview is called, the filter will execute under the jupyter compute engine if engine: knitr is not specified. During the execution, the filter adds two files to the working directory: webr-worker.js and webr-serviceworker.js. These files allow for the webR session to be started and must be present with the rendered output.

Packages

By default, the quarto-webr extension avoids loading or requesting additional packages. Additional packages can be added by including:

webr::install("package")

For example, to install ggplot2, you would need to use:

webr::install("ggplot2")

You can view what packages are available by either executing the following R code (either with WebR or just R):

available.packages(repos="https://repo.r-wasm.org/", type="source")

Or, by navigating to the WebR repository:

https://github.com/r-wasm/webr-repo/blob/main/repo-packages

Known Hiccups

As this is an exciting new frontier, we're learning as we go. Or as my friend Lawrence says, "I like to build airplanes in the air-". Please take note of the following issues:

Stuck at Loading webR...

If webr-worker.js or webr-serviceworker.js are not found when the document loads either at the root / or relative directory, then Loading webR... will appear above the code cell instead of Run code. Please make sure the files are at the same location as the quarto document. For example, the following structure will work:

.
├── demo-quarto-webr.qmd
├── webr-serviceworker.js
└── webr-worker.js

Directly accessing rendered HTML

When using quarto preview or quarto render, the rendered HTML document is being shown by mimicking a server running under https://localhost/. Usually, everything works in this context assuming the above directory structure is followed. However, if you directly open the rendered HTML document, e.g. demo-quarto-web.html, inside of a Web Browser, then the required WebR components cannot be loaded for security reasons. You can read a bit more about the problem in this StackOverflow answer.

There are a few possible solutions to avoid requiring quarto on a local computer to directly open the rendered file:

Speed up webR

When serving webR documents, please try to ensure the COOP and COEP HTTP headers are set to speed up the process:

Cross-Origin-Opener-Policy: same-origin
Cross-Origin-Embedder-Policy: require-corp

For users who host their website with Netlify, add to the netlify.toml configuration file:

[[headers]]
  for = "/directory/with/webr/content/*"

  [headers.values]
    Cross-Origin-Opener-Policy = "same-origin"
    Cross-Origin-Embedder-Policy = "require-corp"

For users who deploy their site with GitHub Pages, please hang tight! By default, GitHub Pages does not allow for headers to be modified. There have been notable efforts by the GitHub Pages community to circumvent this limitation by using coi-servicworker. We're currently testing it out.

For users who administer a server with nginx, the add_header directive can be used to add headers into the configuration file at /etc/nginx/nginx.conf.

server {
  # Enable headers for the webr directory
  location ^~ /directory/with/webr/content {
    add_header "Cross-Origin-Opener-Policy" "same-origin";
    add_header "Cross-Origin-Embedder-Policy" "require-corp";
  }
}

More information may be found in nginx's Serving Static Content.

For additional justificaiton on why the headers are required, please see webR's documentation page for Serving Pages with WebR.

Engine Registration

If using the knitr engine instead of the jupyter engine, there is a known warning that will appear in the render processing output of:

Warning message:
In get_engine(options$engine) :
  Unknown language engine 'webr' (must be registered via knit_engines$set()).

This warning does not prevent or impact the ability of the webr filter to function. Though, we would like to address it at some point since it is not aesthetically pleasing.

Acknowledgements

We appreciate the early testing feedback from Eli E. Holmes and Bob Rudis.

This repository builds ontop of the initial proof of concept for a standalone Quarto HTML document in:

https://github.com/coatless-r-n-d/webR-quarto-demos

The proof of concept leaned heavily on the webR developers public-facing examples:

For the extension, we greatly appreciated insights from:

quarto-webr's People

Contributors

coatless avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.