Is there a sensible way to pre-cache the rendering of cached plots automatically or as batch with a given set of inputs, without relying on the user to go there first?
More explanation of the problem:- let's say I have a Shiny app with a plot which (sometimes) takes a long time (30+ seconds) to render, or a page with multiple plots which together take a long time (in user waiting terms).
I can use renderCachedPlot()
with a memory or disk cache which means that after loading the plot once, the next time, the plot will load much faster (as below).
(This is a toy example, the real example is much more complex)
function(input, output) {
renderCachedPlot(
{
rownums <- seq_len(input$n)
plot(cars$speed[rownums], cars$dist[rownums])
},
cacheKeyExpr = { input$n }
)
}
But that first time is still too long, and if running on a server, users are likely to think it has failed, or get bored and do something else.
If the number input$n
options are small, I could manually go through each input option in order to pre-cache, but then I'd have to do that everytime I update the code; and this seems like something that should be automatable. If the options are in 100+ range then I don't want to do this manually.
If I know all the expected inputs in advance (e.g. all the versions of input$n
), I could alternatively pre-render the plots as PNGs outside the Shiny app, and then renderPNG instead, but that loses the convenience of keeping the plotting code in the Shiny app (when sometimes it is fast enough, at least during testing).
So, is there a way to generate the first plot cache in advance given a set of predefined inputs, e.g. call renderCachedPlot() from an environment which mimics the Shiny environment, but is solely designed to create the cache, and therefore speed up rendering for real users(TM)?
Very grateful for any thoughts on this!