4

I am testing this new-ish method of deploying R shiny apps on dumb web servers by leveraging the WASM technology. I have produced this simple app that doesn't need any data. Its only function is allow the user to upload a .csv file. It then reads the file and displays its content.

Here is the app:

# Load required libraries
library(shiny)
library(shinythemes)

# Define UI
ui <- fluidPage(
  theme = shinytheme("flatly"),
  
  # App title
  titlePanel("CSV File Uploader"),
  
  # Sidebar layout with input and output definitions
  sidebarLayout(
    sidebarPanel(
      fileInput("file", "Choose CSV File", accept = ".csv"),
      tags$hr(),
      checkboxInput("header", "Header", TRUE),
      checkboxInput("stringAsFactors", "Convert strings to factors", TRUE)
    ),
    
    # Show CSV data
    mainPanel(
      tableOutput("contents")
    )
  )
)

# Define server logic
server <- function(input, output) {
  
  # Read CSV file
  data <- reactive({
    req(input$file)
    df <- read.csv(input$file$datapath,
                   header = input$header,
                   stringsAsFactors = input$stringAsFactors)
    return(df)
  })
  
  # Show CSV data
  output$contents <- renderTable({
    data()
  })
}

# Run the application
shinyApp(ui = ui, server = server)

I save this file under the name "app.R" and then package the app and save it in a folder called "app" by using shinylive::export() function.

Testing the app locally using httpuv package works well:

httpuv::runStaticServer("path-to-the-exported-app-folder")

Ultimately, I want to upload the app files to a Github repository and view the app through github pages facility.

Unfortunately, the files are too huge. Viewing the properties of the "app" folder reveals its size is 98mb. How is that possible? Trying to run this app using github pages results in a "time out" error.

What is happening?

2
  • From my limited understanding of webR, this is to be expected - the app folder has to contain the equivalent of a complete version of R as well as the app source. See for example appsilon.com/post/… where they note the exact same thing: “While WebR is still in development, it shows tremendous promise! The loading is definitely still a pain point (over 100mb to set up the environment!) but it will only get better moving forward.” Commented Mar 20, 2024 at 18:25
  • @smartse Thank you for confirming that I am not doing anything wrong. I recognize this technology is still under development, I was just testing the waters. From the look of it, we are still a long way off the mark. I hope we get there soon. Our friends from Python are telling good tales that are making me jealous. Commented Mar 22, 2024 at 6:21

1 Answer 1

6
+50

I've broken this post into sections to address the concerns raised. Hopefully, it helps!

Shinylive App Size

Shinylive apps require around 60 MB or more data due to their dependency on the initial webR base and all {shiny} package dependencies. When moving away from a compute server running Shiny Server to a generic web server, the trade-off lies in the amount of user bandwidth required. So, the users internet and computer is paying for the ability to view the app compared to the old status quo where the host paid a license fee (to Posit), server and maintenance costs.

You can inspect the current list of package dependencies for a basic Shiny app using the following R code:

pkg_db <- tools::CRAN_package_db()
shiny_pkg_dependencies <- tools::package_dependencies(
  packages = c("shiny"),
  recursive = TRUE,
  db = pkg_db
)

shiny_pkg_dependencies
#> $shiny
#>  [1] "methods"     "utils"       "grDevices"   "httpuv"      "mime"       
#>  [6] "jsonlite"    "xtable"      "fontawesome" "htmltools"   "R6"         
#> [11] "sourcetools" "later"       "promises"    "tools"       "crayon"     
#> [16] "rlang"       "fastmap"     "withr"       "commonmark"  "glue"       
#> [21] "bslib"       "cachem"      "ellipsis"    "lifecycle"   "base64enc"  
#> [26] "jquerylib"   "memoise"     "sass"        "digest"      "Rcpp"       
#> [31] "cli"         "magrittr"    "stats"       "graphics"    "fs"         
#> [36] "rappdirs"

Adding more R packages through in-app dependencies will expand this list and increase the download size of each app.

Deploying Automatically with GitHub Actions

Please avoid using the gh-pages branch deployment technique for sharing the app on GitHub pages.

Instead, opt for the GitHub Actions approach, which is preferred because it doesn't store artifacts from converting a Shiny App into a Shinylive App inside the repository. Plus, this approach allows for Shinylive apps to be deployed up to the GitHub Pages maximum of about 1 GB.

Specifically, we're advocating for a file structure of:

.
├── .github
│   └── workflows
│       └── build-and-deploy-shinylive-r-app.yml
├── README.md
└── app.R

Thus, we could place the example app source code into app.R and, then, use the following GitHub Action in .github/workflows/build-and-deploy-shinylive-r-app.yml to build and deploy the shinylive app every time the repository is updated.

on:
    push:
      branches: [main, master]
    release:
        types: [published]
    workflow_dispatch: {}
   
name: demo-r-shinylive-app

jobs:
    demo-website:
      runs-on: ubuntu-latest
      # Only restrict concurrency for non-PR jobs
      concurrency:
        group: r-shinylive-website-${{ github.event_name != 'pull_request' || github.run_id }}
      # Describe the permissions for obtain repository contents and 
      # deploying a GitHub pages website for the repository
      permissions:
        contents: read
        pages: write
        id-token: write
      steps:
        # Obtain the contents of the repository
        - name: "Check out repository"
          uses: actions/checkout@v4

        # Install R on the GitHub Actions worker
        - name: "Setup R"
          uses: r-lib/actions/setup-r@v2
  
        # Install and pin the shinylive R package dependency
        - name: "Setup R dependency for Shinylive App export"
          uses: r-lib/actions/setup-r-dependencies@v2
          with:
            packages:
              cran::[email protected]
  
        # Export the current working directory as the shiny app
        # using the pinned version of the Shinylive R package
        - name: Create Shinylive App from working directory files
          shell: Rscript {0}
          run: |
           shinylive::export(".", "_site")

        # Upload a tar file that will work with GitHub Pages
        # Make sure to set a retention day to avoid running into a cap
        # This artifact shouldn't be required after deployment onto pages was a success.
        - name: Upload Pages artifact
          uses: actions/upload-pages-artifact@v2
          with: 
            retention-days: 1
        
        # Use an Action deploy to push the artifact onto GitHub Pages
        # This requires the `Action` tab being structured to allow for deployment
        # instead of using `docs/` or the `gh-pages` branch of the repository
        - name: Deploy to GitHub Pages
          id: deployment
          uses: actions/deploy-pages@v2

Deployment with this approach requires it to be enabled by:

  • Clicking on the repository's Settings page
  • Selecting Pages on the left sidebar.
  • Picking the GitHub Actions option in the Source drop-down under the Build and Deployment section.
  • Ensuring that Enforced HTTPS is checked.

Example annotation of the repository's Settings page for GitHub Actions deployment

Working Example

For a comprehensive example demonstrating deployment, documentation, and a functional version of your app, refer to the following:

Some quick screenshots that describe whats up:

Example of the working shinylive app

Summary of the deployment of the shinylive app

Sign up to request clarification or add additional context in comments.

4 Comments

I tried to reproduce these steps to publish the app using gh actions but I am still getting the "Time out" error. Same thing happens when I click on your provided URL as well...
Can I get links to the repo and app?
This is the link to the repo: github.com/fgashakamba/test-shiny-app and this is the URL of the app: fgashakamba.github.io/test-shiny-app
So, the repository looks good and matches the outlined approach. I'm even able to load and supply data, c.f. imgur.com/O8iWb0p . So, I'm wondering how fast is your internet connection? There might be a download time out happening... Are there any errors listed in the web developer console?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.