【问题标题】:R Shiny Async with Progress BarR Shiny Async 与进度条
【发布时间】:2018-07-03 15:27:27
【问题描述】:

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

【问题讨论】:

    标签: r asynchronous shiny


    【解决方案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)
    }
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-23
    • 1970-01-01
    • 2020-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-16
    • 1970-01-01
    相关资源
    最近更新 更多