shiny Plot Caching

Published by onesixx on
output$plot <- renderCachedPlot(
      # Plotting code here...
    cacheKeyExpr = { list(input$n, dataset()) }

improve the performance of your plots.
- using R’s base graphics instead of a plotting package
- using JavaScript graphics that render on the client instead of static plots that render on the server
- change the type of plot
( e.g. switching from ggplot2::geom_point to ggplot2::geom_hex )
- cache plots with renderCachedPlot()
코드변경을 최소로 하여

ui <- \tfluidPage(
\t\t\tsliderInput("n", "Number of points", 4, 32, value = 8, step = 4),
\t\t\tsliderInput("alpha", "Number of points", 0, 1, value = 0.5)
server <- function(input, output, session) {
\toutput$plot <- renderCachedPlot({
\t\tSys.sleep(2)  # Add an artificial delay
\t\trownums <- seq_len(input$n)
\t\taV <- isolate(input$alpha)
\t\tcars [rownums,] %>% ggplot(aes(x=speed, y=dist)) + geom_point(alpha=aV) +
\t\t\t\t\t\t\t\t\t\t\t\t\txlim(range(cars$speed))+ ylim(range(cars$dist))
\t},cacheKeyExpr = {c(input$alpha, input$n)})


shinyApp(ui, server)

cacheKeyExpr :
reactive expressions / reactive values / eventReactive() / observeEvent()

 the value from cacheKeyExpr is actually serialized and then hashed,
and the resulting hash value is used as the key.

If you are hashing a large object, this may take a non-negligible amount of time

Categories: R-Shiny


Blog Owner

Notify of

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x