Edit
Caching of images created with renderPlot()/plotOutput()
is supported since shiny 1.2.0.
The solution below behaves similar to the following usage of renderCachedPlot()
.
output$plot <- renderCachedPlot(
expr = {
histfaithful(bins = input$bins, col = input$col)
},
cache = diskCache()
)
renderCachedPlot()
allows caching in memory and on disk with sensible defaults. The rules for generating hash keys can be customized and by default digest::digest()
is used for all reactive expressions that appear in expr
.
The solution below demonstrates how a subset of these features (caching on disk) can be implemented with a shiny module. The basic strategy is to use
digest::digest()
to create cache keys based on arguments sent to a plot function
do.call()
to pass the arguments to the plot function unless the key created from digest()
signifies that the image is already cached
grDevices::png()
to capture an image from the call to do.call()
and add it to the cache
shiny::renderImage()
to serve images from the cache
Original answer
Although both answers to this question are very good, I wanted do add another one using shiny modules. The following module takes a plotfunction and a reactive version of it's arguments as inputs. In the end do.call(plotfun, args())
is used to create the plot.
library(shiny)
cachePlot <- function(input, output, session, plotfun, args, width = 480, height = 480,
dir = tempdir(), prefix = "cachedPlot", deleteonexit = TRUE){
hash <- function(args) digest::digest(args)
output$plot <- renderImage({
args <- args()
if (!is.list(args)) args <- list(args)
imgpath <- file.path(dir, paste0(prefix, "-", hash(args), ".png"))
if(!file.exists(imgpath)){
png(imgpath, width = width, height = height)
do.call(plotfun, args)
dev.off()
}
list(src = imgpath)
}, deleteFile = FALSE)
if (deleteonexit) session$onSessionEnded(function(){
imgfiles <- list.files(dir, pattern = prefix, full.names = TRUE)
file.remove(imgfiles)
})
}
cachePlotUI <- function(id){
ns <- NS(id)
imageOutput(ns("plot"))
}
As we can see, the module deletes the image files created if needed and gives the option to use a custom caching-directory in case persistent caching is needed (as it is in my actual usecase).
For a usage example, I'll use the hist(faithful[, 2])
example just like Stedy.
histfaithful <- function(bins, col){
message("calling histfaithful with args ", bins, " and ", col)
x <- faithful[, 2]
bins <- seq(min(x), max(x), length.out = bins + 1)
hist(x, breaks = bins, col = col, border = 'white')
}
shinyApp(
ui = fluidPage(
inputPanel(
sliderInput("bins", "bins", 5, 30, 10, 1),
selectInput("col", "color", c("blue", "red"))
),
cachePlotUI("cachedPlot")
),
server = function(input, output, session){
callModule(
cachePlot, "cachedPlot", histfaithful,
args = reactive(list(bins = input$bins, col = input$col))
)
}
)