有没有办法在 Shiny 中预先缓存输出?

Ice*_*can 5 r 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)
Run Code Online (Sandbox Code Playgroud)

笔记:

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

编辑:

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

tho*_*hal 6

如何使用持久缓存,运行应用程序一次,根据需要手动更改所有输入(我还包括一个自动化版本,我对此不太满意,可能会发生 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)
Run Code Online (Sandbox Code Playgroud)