R Shiny Async with Progress Bar

纵然是瞬间 提交于 2021-02-08 15:19:04

问题


Async processing in Shiny is supposed to take a long-running function and give control back to the user. However, it would still be nice to let the user know that the computation is running in the background. I cannot figure out how to structure the async process to run in the background and still display a progress indicator. Below is the example code I've been fiddling with. I think the progress indicator is one issue, but also the creation of the table doesn't seem to be working with async processing.

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

回答1:


check out the package 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)
}


来源:https://stackoverflow.com/questions/51158214/r-shiny-async-with-progress-bar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!