r - R Shiny Async 与进度条
问题描述
Shiny 中的异步处理应该采用长时间运行的功能并将控制权交还给用户。但是,让用户知道计算在后台运行仍然会很好。我无法弄清楚如何构建异步进程以在后台运行并仍然显示进度指示器。下面是我一直在摆弄的示例代码。我认为进度指示器是一个问题,但表的创建似乎也不适用于异步处理。
library(shiny)
library(future)
library(promises)
plan(multiprocess)
shinyApp(
ui = basicPage(
tableOutput('table'),
actionButton('goTable', 'Go table')
),
server = function(input, output, session) {
table_data <- reactive({
# make reactive to button click
input$goTable
# Create a Progress object
progress <- shiny::Progress$new()
progress$set(message = "Building Table", value = 0)
# Close the progress when this reactive exits (even if there's an error)
on.exit(progress$close())
# build up the table data
future({
this_dat <- NULL
for(i in 1:5){
Sys.sleep(1)
this_dat <- rbind(this_dat, data.frame(i=i))
# increment progress
progress$inc(1/5)
}
})
return(this_dat)
})
output$table <- renderTable({
table_data()
})
}
)
解决方案
查看包ipc:
## Only run examples in interactive R sessions
if (interactive()) {
library(shiny)
library(future)
plan(multiprocess)
ui <- fluidPage(
actionButton("run","Run"),
tableOutput("dataset")
)
server <- function(input, output, session) {
dat <- reactiveVal()
observeEvent(input$run, {
progress <- AsyncProgress$new(session, min=1, max=15)
future({
for (i in 1:15) {
progress$set(value = i)
Sys.sleep(0.5)
}
progress$close()
cars
}) %...>% dat
NULL
})
output$dataset <- renderTable({
req(dat())
})
}
shinyApp(ui, server)
}
推荐阅读
- windows - 通过 openssl 创建根证书和服务器证书以在 Web 服务器上启用 https 在 Chrome 浏览器上显示 Not Secure - 所以 HTTPS 不起作用
- youtube - 我们可以在机器人框架中的视频卡上启用全屏选项吗?
- javascript - 网页登录后如何获取用户ID令牌
- redis - 如果键数量增加,Redis 性能
- android - 在应用启动时从 Web 服务加载数据
- python - 将背景颜色更改为黑色
- vb.net - 关闭表单时如何保存轨迹栏的最后一个位置/值?
- javascript - 在 .env 文件中创建的环境变量未定义
- javascript - 点击时自动隐藏导航
- python - 如何用其他数据框中的 id 替换数据框中的 2 列值?