具有进度条的R Shiny Async

时间:2018-07-03 15:27:27

标签: r asynchronous shiny

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()
    })    
  }
)

2 个答案:

答案 0 :(得分:1)

检出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)
}

答案 1 :(得分:-1)

我从来没有制作过一个异步闪亮的应用程序,但是如果它有用的话:ipc包中有一个小插图,涵盖了adding a progress bar to an async operation

Ian Fellows在rstudioconf 2019上发表了有关此内容及更多内容的演讲:Don't let long running tasks hang your users: introducing ipc for Shiny