data vizshiny

Asynchronous download in Shiny

Since the release of Shiny 1.1.0, we have the option to use asynchronous operations. I will describe why and how I have implemented an asynchronous download.

Problem

R is single threaded by nature. You can install R on a powerful server with 64 CPU cores, but R will only use one of them by default. This can make it challenging to get a good performance of your script cq application.

For a client, I have created a PDF report from their shiny app. Since there are many plots and datatables in the application, the report is quite big. It can take about a minute on shinyapps.io. Time in which another user possibly cannot access the dashboard.  This depends on your deployment configuration and the current amount of users that are connected to your dashboard.

Solution

To overcome this issue, I have rewritten my report generation code in a way that it’s performed asynchronously.  To accomplish this I have used the following libraries:

  • future: used to perform the long running report operation in a worker process that runs in the background, leaving the (main) Shiny processes free to serve other users in the meantime.
  • promises: to handle the result of the long running report operation back in the Shiny process, where we can display the result (downloaded files) back to the user.
  • ipc: used to display an (Async compatible) progress bar.
Testing

To test that it works, open the shiny app (see link below) in 2 browser tabs. When you click on ‘Download Report’ in the 1st tab, you still should be able to use the application in the 2nd tab.

You can view the app including source code on my Shinyapps.io

 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *