R Shiny at IQSS
  • Introduction
  • Getting started
    • Determining your resource requirements
    • Applying for an account
  • Deploying
    • Setup your environment
    • Deploying an example Shiny app
    • Bootstrapping a new Shiny app or migrating a pre-existing Shiny application
    • Developing locally with RStudio
    • Developing locally using Docker
    • Speeding up deployments with Packrat
  • Configuration
    • Setting up a custom domain for your Shiny app
    • Installing additional system-level packages
    • Limiting the number of R Threads
  • Best Practices
    • File storage
    • Using promises
    • Adding routes
  • Troubleshooting
    • Resolving Application Error screen
    • Viewing app logs
    • Viewing resource consumption
    • Configuring Shiny session auto-reconnect
    • Getting support
Powered by GitBook
On this page
  • What is asynchronous programming?
  • Asynchronous R documentation
  1. Best Practices

Using promises

PreviousFile storageNextAdding routes

Last updated 5 years ago

As explained in , R is single-threaded.

While our Shiny toolchain optimizes your application by running multiple R processes, using promises will maximize your performance far greater than our balancing between multiple R processes.

What is asynchronous programming?

Synchronus code executes linearly. The following reads a CSV from the included URL.

value <- read.csv("http://example.com/data/data.csv")
a <- 1+2

Before a can be assigned the value of 3, R will execute an HTTP request to http://example.com/data/data.csv, download the CSV, and store it in the variable value. The R process is blocked until this is completed. There is no reason to wait for the CSV to complete downloading before continuing with the rest of your application's code, in particular because network functions can be offloaded to the operating system's event handler (libuv).

promise <- future(read.csv("http://example.com/data/data.csv"))
a <- 1+2

The above will download the CSV in a non-blocking manner, meaning that a will be assigned 3 even if read.csv hasn't completed yet.

Asynchronous programming will execute an instruction, designated as asynchronous by the promise keyword, in a different thread, without waiting for it's result. You can offload long-running operations to a subsidiary R thread spawned by the promise library.

Asynchronous R documentation

If you're processing CSVs, executing long-running code in your R Shiny app, you should use promises to speed up your application.

The following tutorials detail how to develop asynchronous R application.

Limiting the number of R Threads
Working with promises in R
Async programming in R and Shiny
Case study: converting a Shiny app to async
How to use the new R promises package