首页 > 解决方案 > 有没有办法在 Shiny 中预缓存输出?

问题描述

我在下面有闪亮的应用程序。我第一次选择任何给定的数字时,加载结果需要 3 秒。由于bindCache,如果我稍后选择相同的数字,我会立即得到结果。

但是,我不想手动选择所有 10 个数字,只是为了让我的应用在呈现之前做出响应。有没有办法提前缓存一组输入?在此示例中,我想缓存input$num值 1 到 10 的结果。在实际应用程序中,大约有 5 个输入,每个输入有 5 个可能的值,用于 25 个我想要缓存的可能结果。

library(shiny)

ui <- fluidPage(
  sliderInput('num', 'Pick a number:', min = 1, max = 10, value = 1),
  textOutput('out')
)

server <- function(input, output, session) {
 output$out <- reactive({
   Sys.sleep(3)
   paste("Your number is:", input$num)
 }) %>% bindCache(input$num)
}

shinyApp(ui, server)

笔记:

一种回应可能是我应该手动预先计算这些结果。在真正的应用程序中,大部分时间来自我预先创建的表格gt::render_gt。我相信这些功能只能在反应式上下文中使用(意味着只能在闪亮的应用程序中使用?)gt::gt_outputgt

编辑:

作为旁注,我最初的问题是通过使用gt::as_raw_html在闪亮应用程序之前的一步中呈现表格来解决的。尽管如此,仍然留下这个问题,因为它有时在其他情况下是一个问题。

标签: rshiny

解决方案


如何使用持久缓存,运行应用程序一次,根据需要手动更改所有输入(我还包括一个自动化版本,我对此并不完全满意,可能会发生 b/c 竞争条件),然后在随后的运行中你是否正确缓存了所有值?

library(shiny)
library(magrittr)
## change path to a non temp diretcory to keep that even after reboot
shinyOptions(cache = cachem::cache_disk(file.path(dirname(tempdir()), 
                                        "myapp-cache")))

xl <- 1:3
yl <- 1:3

ui <- fluidPage(
   sliderInput("x", "x", min(xl), max(xl), min(xl), 1),
   sliderInput("y", "y", min(yl), max(yl), min(yl), 1),
   verbatimTextOutput("z"),
   actionButton("fill", "Fill Cache")
)

server <- function(input, output, session) {
   idx <- idy <- 1
   r <- reactive({
      message("Doing  expensive computation...")
      Sys.sleep(2) ## simulate expensive op
      input$x + input$y
   }) %>% bindCache(input$x, input$y)

   observe({
      req(input$fill)
      if (idx != length(xl) + 1 || idy != length(yl)) {
         ## need the invalidateLater approach 
         ## to allow shiny reacting on the change
         ## not sure whether we cannot trip over race conditions
         ## recommendation: do it once by hand (it's persistent anyways ;)
         invalidateLater(500, session) 
         if (idx == length(xl) + 1) {
            message("Updating y:", idy)
            idx <<- 1
            idy <<-  idy + 1
            updateSliderInput(session, "y", value = yl[[idy]])
         } else {
            message("Updating x:", idx)
            updateSliderInput(session, "x", value = xl[[idx]])
            idx <<- idx + 1
         }
      }
   })

   output$z <- renderText(r())
}

## Start app and set all values
shinyApp(ui, server)

## Close app and restart
## Cache is now filled
shinyApp(ui, server)

推荐阅读