Examples for 'shiny::renderCachedPlot'


Plot output with cached images

Aliases: renderCachedPlot

Keywords:

### ** Examples

## Only run examples in interactive R sessions
if (interactive()) {

# A basic example that uses the default app-scoped memory cache.
# The cache will be shared among all simultaneous users of the application.
shinyApp(
  fluidPage(
    sidebarLayout(
      sidebarPanel(
        sliderInput("n", "Number of points", 4, 32, value = 8, step = 4)
      ),
      mainPanel(plotOutput("plot"))
    )
  ),
  function(input, output, session) {
    output$plot <- renderCachedPlot({
        Sys.sleep(2)  # Add an artificial delay
        seqn <- seq_len(input$n)
        plot(mtcars$wt[seqn], mtcars$mpg[seqn],
             xlim = range(mtcars$wt), ylim = range(mtcars$mpg))
      },
      cacheKeyExpr = { list(input$n) }
    )
  }
)



# An example uses a data object shared across sessions. mydata() is part of
# the cache key, so when its value changes, plots that were previously
# stored in the cache will no longer be used (unless mydata() changes back
# to its previous value).
mydata <- reactiveVal(data.frame(x = rnorm(400), y = rnorm(400)))

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      sliderInput("n", "Number of points", 50, 400, 100, step = 50),
      actionButton("newdata", "New data")
    ),
    mainPanel(
      plotOutput("plot")
    )
  )
)

server <- function(input, output, session) {
  observeEvent(input$newdata, {
    mydata(data.frame(x = rnorm(400), y = rnorm(400)))
  })

  output$plot <- renderCachedPlot(
    {
      Sys.sleep(2)
      d <- mydata()
      seqn <- seq_len(input$n)
      plot(d$x[seqn], d$y[seqn], xlim = range(d$x), ylim = range(d$y))
    },
    cacheKeyExpr = { list(input$n, mydata()) },
  )
}

shinyApp(ui, server)


# A basic application with two plots, where each plot in each session has
# a separate cache.
shinyApp(
  fluidPage(
    sidebarLayout(
      sidebarPanel(
        sliderInput("n", "Number of points", 4, 32, value = 8, step = 4)
      ),
      mainPanel(
        plotOutput("plot1"),
        plotOutput("plot2")
      )
    )
  ),
  function(input, output, session) {
    output$plot1 <- renderCachedPlot({
        Sys.sleep(2)  # Add an artificial delay
        seqn <- seq_len(input$n)
        plot(mtcars$wt[seqn], mtcars$mpg[seqn],
             xlim = range(mtcars$wt), ylim = range(mtcars$mpg))
      },
      cacheKeyExpr = { list(input$n) },
      cache = memoryCache()
    )
    output$plot2 <- renderCachedPlot({
        Sys.sleep(2)  # Add an artificial delay
        seqn <- seq_len(input$n)
        plot(mtcars$wt[seqn], mtcars$mpg[seqn],
             xlim = range(mtcars$wt), ylim = range(mtcars$mpg))
      },
      cacheKeyExpr = { list(input$n) },
      cache = memoryCache()
    )
  }
)

}
Error in loadNamespace(name): there is no package called 'webshot'
## Not run: 
##D # At the top of app.R, this set the application-scoped cache to be a memory
##D # cache that is 20 MB in size, and where cached objects expire after one
##D # hour.
##D shinyOptions(cache = memoryCache(max_size = 20e6, max_age = 3600))
##D 
##D # At the top of app.R, this set the application-scoped cache to be a disk
##D # cache that can be shared among multiple concurrent R processes, and is
##D # deleted when the system reboots.
##D shinyOptions(cache = diskCache(file.path(dirname(tempdir()), "myapp-cache"))
##D 
##D # At the top of app.R, this set the application-scoped cache to be a disk
##D # cache that can be shared among multiple concurrent R processes, and
##D # persists on disk across reboots.
##D shinyOptions(cache = diskCache("./myapp-cache"))
##D 
##D # At the top of the server function, this set the session-scoped cache to be
##D # a memory cache that is 5 MB in size.
##D server <- function(input, output, session) {
##D   shinyOptions(cache = memoryCache(max_size = 5e6))
##D 
##D   output$plot <- renderCachedPlot(
##D     ...,
##D     cache = "session"
##D   )
##D }
##D 
## End(Not run)

[Package shiny version 1.5.0 Index]