Using R in the Browser: WebR

Author

Wesley Brooks

Published

25 May, 2023

Overview

The URL for this workshop is d-rug.github.io/workshop-webr

WebR is a tool that lets you run R code in the browser on the client side. You may know that Web browsers only know three languages: html, css, and javascript. So, WebR is a port of the R source code to javascript. This is accomplished by some tools (beyond the scope of this presentation) that automatically convert C code to an intermediate language called WebAssembly.

WebR is very new (the first release was in March 2023) ao there are still rough edges. But it works pretty smoothly with Quarto, which is how we build these workshop websites.

Anyway, the upshot is that these workshop sites can now be interactive! Rather than copying code from the site and pasting it into R, we can now run the code directly in the site. Let’s look at an example:

WebR example

This example will show off some basic R functions. First, let’s read in some data.


I’ve loaded data directly from a web address. Your computer’s file system is not visible to WebR (for security, websites aren’t allowed to see files on your computer). So you must either load data directly from the web, or upload your data to a virtual file system that I haven’t yet figured out (reminder that WebR is very new).


Data is maintained between chunks.


Just like any R installation, the base functions are available immediately. Using functions from packages requires installing and loading the packages first, and the package installations system is unique (for the same reason as the file system is unique - your browser isn’t allowed to see most files on your computer). Installing packages via webr::install() works, but it is slower than you’re used to.


Learning more

The official WebR site is nice, but I find it to be a bit confusing for a non-expert web developer. There’s also this list of resources about WebR with better variety and more examples to draw from. And, you can always post questions on the D-RUG question forum for help from the group.

Using WebR

If you want to include WebR in your own website, I would recommend that you start by using the D-RUG WebR workshop template. That template is for a Quarto project, so you will need to install Quarto to use it, but I recommend using Quarto to build your website, anyway.