WebR/Shinylive Overview

George Stagg

R in the browser: webR

The webR logo

The webR project is a version of the R interpreter built for WebAssembly.

Execute R code directly in a web browser, without a supporting R server. Alternatively, run an R process server-side using Node.js

How it works: WebAssembly (2017)

  • A portable binary code format
  • Enables high-performance applications on web pages
  • Near-native execution speed
  • Supported by most modern browsers

  • Containerisation
  • Sandboxing
  • Interactivity
  • Reproducibility
  • Software portability
  • Universal binaries

R Consortium Submission Working Group

πŸ”— https://rconsortium.github.io/submissions-wg/

  • Create clinical trial submission deliverables with open-source software

  • Consists of Industry and Regulatory members

  • Pilot 3: Successful Shiny app submission to FDA reviewers

Hosting a Shiny app

Traditional Shiny App

Bundle and distribute app source?

  • Transfer source and data to some other machine.
  • Run the app with a local Shiny server.


Requires a reproducible workflow and software installation:

  • R/Python interpreter, of the correct version.
  • Software and environment control: Docker, rix, renv, venv, etc.
  • Software development tools: RStudio, VSCode, Positron.
  • Knowledge and experience: Shiny runApp(), debugging.

Shinylive πŸ”— https://shinylive.io/r/

Shinylive App

Convert a Shiny app to Shinylive

Install the Shinylive R package:

install.packages("shinylive")


Convert the app:

shinylive::export("myapp", "site")

Binary bundle ready to transfer to another machine or host on a static web service.


Run the application:

httpuv::runStaticServer("site")

or…

python -m http.server
npx http-server 

Shinylive in Quarto

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor
incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis
nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo.

```{shinylive-r}
#| standalone: true
library(shiny)

# Create Shiny UI
ui <- [...]

# Create Shiny server function
server <- function(input, output, session) {
  [...]
}

# Build Shiny app
shinyApp(ui = ui, server = server)
```

Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu
fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in
culpa qui officia deserunt mollit laborum.
#| standalone: true
#| viewerHeight: 700

library(shiny)
library(bslib)

theme <- bs_theme(font_scale = 1.5)

# Define UI for app that draws a histogram ----
ui <- page_sidebar(theme = theme,
  sidebar = sidebar(open = "open",
    numericInput("n", "Sample count", 50),
    checkboxInput("pause", "Pause", FALSE),
  ),
  plotOutput("plot", width=1100)
)

server <- function(input, output, session) {
  data <- reactive({
    input$resample
    if (!isTRUE(input$pause)) {
      invalidateLater(1000)
    }
    rnorm(input$n)
  })
  
  output$plot <- renderPlot({
    hist(data(),
      breaks = 30,
      xlim = c(-2, 2),
      ylim = c(0, 1),
      xlab = "value",
      freq = FALSE,
      main = ""
    )
    
    x <- seq(from = -2, to = 2, length.out = 500)
    y <- dnorm(x)
    lines(x, y, lwd=1.5)
    
    lwd <- 5
    abline(v=0, col="red", lwd=lwd, lty=2)
    abline(v=mean(data()), col="blue", lwd=lwd, lty=1)

    legend(legend = c("Normal", "Mean", "Sample mean"),
      col = c("black", "red", "blue"),
      lty = c(1, 2, 1),
      lwd = c(1, lwd, lwd),
      x = 1,
      y = 0.9
    )
  }, res=140)
}

# Create Shiny app ----
shinyApp(ui = ui, server = server)

WebAssembly R packages

Binary R packages for WebAssembly are available from CRAN-like repositories

  • Recent improvements:
    • Improved bundling
    • No external dependencies, long-term reproducibility

rwasm package hex logo

Future work and current issues

  • Not all R packages work under WebAssembly.

  • Requires a web server, even for local applications.

  • There will always be good reasons to use a traditional Shiny deployment.

  • Browser security restrictions: limited networking & database access, no raw socket access.

  • There are no secrets with a Shinylive app! Anything in the app source can be viewed.

πŸ”— webR demo website

https://webr.r-wasm.org/v0.4.2/

🌎 Shinylive examples

https://shinylive.io/r/

https://shinylive.io/py/

πŸ“™ Documentation

https://docs.r-wasm.org/webr/v0.4.2/

https://github.com/posit-dev/shinylive

https://github.com/quarto-ext/shinylive