webR: R without the hassle (almost)

Meet John

He just arrived to the Communicating with Data tutorial a bit late and realized that he has forgotten his laptop.

He looks up at the Main Display to see the following task.

This would need a laptop with R running on it…

But,

He has brought his tab that has a web browser on it though.

Can he make it through the tutorial?

webR

overwatch meme

File management

John decides to download the dataset from the tutorial website and uploads it to the file explorer in webR

Note

You can avoid the download and upload steps by running download.file and specifying the URL and download location. When in doubt run getwd() to ensure you are in the right folder.

Package management

John tries to run library(tidyverse) by instinct only to find out that tidyverse needs to be installed.

Note

Not all packages that you are used to is available in webR. You can check the available packages

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

or by checking out the Github page of webR

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

You can install packages by running webr::install("tidyverse"). Or you can simply run library(tidyverse) and webR will ask whether to install the package.

Data management

After installing the packages he dives right into writing the following code.

Note

The data processing speed depends on the device that he is using to access the webR site, in this case a tab

library(tidyverse)

data <- read_csv("census-birthplace.csv") |>
  filter(!birth %in% c("Total", "Not Stated", "Other", "Australia"), 
          census == 2021) |>
  slice_max(count,n = 5) |>
  mutate(birth = fct_reorder(birth, count))

Plot management

  • John decides to create a few simple plots with plot()
  • The plot area in the webR demo site keeps a track of the plots as well.
data |>
  ggplot(aes(x = count, y = birth, fill = birth)) +
  geom_bar(stat = "identity", show.legend = FALSE) +
  scale_fill_manual(
    values = c("darkgray", "darkgray", "darkgray","navy", "darkgray")
  )+
  labs(
    title = "India now second... ", 
    x = "Num of Aussies",
    y = "Birth Place",
    caption = "Data Sauce"
  )

Note

You can also create sf plots now with webR 0.2!

library(sf)
nc <- st_read(system.file("shape/nc.shp", package="sf"))
plot(st_geometry(nc), col = sf.colors(12, categorical = TRUE), border = 'grey', 
     axes = TRUE)
plot(st_geometry(st_centroid(nc)), pch = 3, col = 'red', add = TRUE)

Stress management

  • John managed to make it out of the tutorial happily.
  • Thanks to webR his grades begins to skyrocket.
  • His back pains are gone after bringing just the tab to classes.
  • John goes on to become a lecturer at Monash University where his back pain eventually comes back up again.

What exactly is webR

It’s R running on a web browser!

But doesn’t Shiny let us show R command output on a web page?

webR vs Shiny


Executes R commands on the client browser through WebAssembly

Executes R commands on a dedicated server machine using the R interpreter


Wait, hol’ up… What is WebAssembly?

What is WebAssembly (WASM)

  • Simply put, It is low level programming instruction format (the kind that computers understand) that works on top of a virtual machine inside web browsers.

  • By compiling R and it’s ecosystem of packages into this instruction format (web browser tongue), we can run R commands on this virtual machine inside the web browser!

webR for teaching

There’s a quarto extension that allows you to have webR inside your Quarto documents!

You can install the extension by running the following

quarto add coatless/quarto-webr
  • And it supports webR 0.2!

Caution

The above Quarto extension works nicely with Quarto documents but for revealjs presentations there are a few extra steps that are needed.

Short demo

Head on over to https://webr-hacky-hour.netlify.app/ and try it out now!








webR for research

  • If you have made a Shiny application for your research with heavy computations you can offload it to the client browser

Warning

A bit of Javascript knowledge will be needed to work through this!

  • If you have a made a new package that you want to have users try out during a presentation at a conference

Warning

You would need to compile the package into a format that can support WASM, which would require a development environment running Linux. There’s a bit of learning curve here, so tread carefully.