[英]R Shiny Async with Progress Bar
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)
}
我從未制作過異步閃亮的應用程序,但萬一它很有用:ipc包中有一個小插圖,它包括為異步操作添加進度條 。
Ian Fellows在2019年的rstudioconf上發表了關於這個問題的演講: 不要讓長時間運行的任務掛起你的用戶:為Shiny引入ipc 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.