shiny Plot Caching

Published by onesixx on

https://shiny.rstudio.com/articles/plot-caching.html
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(
\tsidebarLayout(
\t\tsidebarPanel(
\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)
\t\t),
\t\tmainPanel(
\t\t\tplotOutput("plot")
\t\t)
\t)
)
\t
server <- function(input, output, session) {
\t
\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\t\t
\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

onesixx

Blog Owner

Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x