Search code examples
rshinyshiny-server

caching plots in R/Shiny


Just wondering if there are tricks/ways in which I could cache the plots being generated through our shiny app.

Background:

We are doing somewhat compute intensive computations which finally result in a plot. I am already caching(using memoise) the computations done, globally in shiny but it still takes about .75 seconds to render a plot. I was just wondering if we can decrease that time by removing the time it takes to render an image and if there are slick ways of already doing it.

More details:

I am using grid to create the plot(heatmap in this case. Ideally would like the caching to be disk based as storing plots in memory wont scale up.

Thanks! -Abhi


Solution

  • Assuming you are using ggplot (which with Shiny, I would bet is a fair assumption).

    1. Create an empty list to store your grob, say Plist.
    2. When a user request a graph, create a string hash based on the shiny inputs
    3. Check if the graph is already saved, eg hash %in% names(Plist)
    4. If yes, serve up that graph
    5. If no, generate the graph, save the grob to the list, name the element by the hash, eg, Plist[hash] <- new_graph