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)
笔记:
一种回应可能是我应该手动预先计算这些结果。在真正的应用程序中,大部分时间来自我预先创建的表格gt::render_gt
。我相信这些功能只能在反应式上下文中使用(意味着只能在闪亮的应用程序中使用?)gt::gt_output
gt
编辑:
作为旁注,我最初的问题是通过使用gt::as_raw_html
在闪亮应用程序之前的一步中呈现表格来解决的。尽管如此,仍然留下这个问题,因为它有时在其他情况下是一个问题。
解决方案
如何使用持久缓存,运行应用程序一次,根据需要手动更改所有输入(我还包括一个自动化版本,我对此并不完全满意,可能会发生 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)
推荐阅读
- ms-access - 使用 Dlookup 时获取空值
- javascript - 将属性与其键合并到数组的一个索引中
- c++ - constexpr C++ 容器——一种 push_back 到 initializer_list 或类似的方法
- xamarin.forms - Xamarin.Forms 中 ListView 内网格布局中的条件格式项目
- javascript - 为什么递归函数不从js中的main函数继承变量值?
- java - 可以用注释类来限制类吗?
- python - 无服务器 Flask 应用程序在本地工作,在 AWS Lambda 上部署时生成错误
- security - 在稀疏重复值中使用静态 IV 的实际风险?
- apache-spark - 为什么在 Spark 中使用 insertInto 函数写入 hive 阶段目录需要更多时间?
- typo3 - 如何使用 html 模板创建完整的页面