Could loading time of a large Shiny app be reduced by reading in most of the code externally?

I'm dealing with a large Shiny app whose collective codebase is a few thousand lines. Once loaded, the app works very well and runs smoothly. However, it takes on the order of a couple minutes to load.

This results in the 'Please wait..." screen users see as it chugs through all the code on startup.

Could a large part of my app.R code be loaded in externally? I've developed apps in the past that import and work on decently-sized spreadsheets using googlesheets4.

Could something like googledrive be used to read in .R files? I wasn't really able to find much mention of this idea in the wild.

I would say the only way to know for sure is to test, but:
1/ a few thousand lines of code doesn't seem that much, if they run fast
2/ if running these lines is slow, then no matter where they are stored it will still be slow.

A few random thoughts:

Are you loading any data apart from that code, or is all the data coming from that code? Loading big data files can be slower.

Is this code doing some heavy computations, perhaps generating a lot of data that fills memory? Does it run fast on your computer? Is there a way to precompute all that so that you can directly load the data when starting the app? Could caching (of data or plots) help?

Are you testing on your computer within RStudio, or on a server (e.g. shinyapps.io)? Could the server have limitations (of RAM or CPU) that make some computations much sower?

Have you tried using shinyloadtest and profvis to better target the problem?

Hello,

You might want to read-up on scoping rules for Shiny apps:
https://shiny.rstudio.com/articles/scoping.html

In essence, the place where you put some variables / code defines whether they need to be loaded for every single session again (i.e. unique) or only once (i.e. global).

You might have to disentangle some of your existing code, but this can really help reducing loading times if implemented correctly and the analysis allows it.

Hope this helps,
PJ

1 Like