/peeking-at-an-r-shinylive-app-source-code

Demo code showing how to obtain an R Shinylive app source

Primary LanguageJavaScript

My Project

Peeking at an R Shinylive App Source

Interested in learning what code is being used inside a serverless R Shinylive application? If so, this is the repository for you! Here’s a summary of what you can find in the repository:

Background: R Shinylive Apps

R Shinylive is a technology from Posit that combines Shiny, webR, and WebAssembly to enable Shiny applications to be run entirely in a web browser without the need for a compute server. In essence, an R Shinylive application does not require a Shiny server to run. Instead, it can run anywhere that can serve static HTML web page files.

Overview

For this exercise, we’re going to focus on being able to obtain the R Shinylive app source that is powering the webR binary R package repository dashboard.

app.json

R Shinylive app data is stored using [JavaScript Object Notation (JSON)] inside of an app.json file. For each file that is converted from a regular R Shiny app, there is an entry inside of a JSON array that contains a subfield with three key-value pairs:

  1. "name": The name of the file that was converted.
    • app.R or two separate entries with ui.R or server.R.
  2. "content": The escaped contents of the file that was created.
  3. "type": The kind of data stored inside "content".
    • This will likely contain the value of "text".

Single-file app: app.R

For instance, if we have a single file Shiny app in app.R and we have an RStudio project called shinylive-to-shiny, then we would expect two entries in the app.json:

[
  {
    "name": "app.R",
    "content": "library(shiny)\nlibrary(dplyr)\n ... \nshinyApp(ui = ui, server = server)\n",
    "type": "text"
  },
  {
    "name": "shinylive-to-shiny.Rproj",
    "content": "Version: 1.0\n\nRestoreWorkspace: Default ...\n\nAutoAppendNewline: Yes\n",
    "type": "text"
  }
]

Multi-file app: ui.R and server.R

For a two-file Shiny app that uses ui.R and server.R alongside an RStudio project called shinylive-to-shiny, we would expect to have three different entries array stored in app.json:

[
  {
    "name": "server.R",
    "content": server <- function(input, output) { ... }\n",
    "type": "text"
  },
  {
    "name": "ui.R",
    "content": "ui <- fluidPage(\n  sidebarLayout( ... )\n  )\n)",
    "type": "text"
  },
  {
    "name": "shinylive-to-shiny.Rproj",
    "content": "Version: 1.0\n\nRestoreWorkspace: Default ...\n\nAutoAppendNewline: Yes\n",
    "type": "text"
  }
]

Extracting a Shinylive App

Given the above structure, we can use existing R packages and features to obtain the app.json.