在独立功能运行时,在Shiny中更新progressBar

时间:2017-05-19 07:54:02

标签: r modal-dialog shiny progress-bar

此问题与this one有关。

好吧,我在模态中有progressBar,我可以使用for更新进度。现在我需要在执行函数时递增progressBar。我知道我可以随时运行progressBar更新,但是我需要进度条才能在执行函数时增加。

我试过这个,但它显示progressBar更新为100,如果函数执行很重,看起来页面已冻结,并且还显示默认的progressDialog

UI:

tags$script("Shiny.addCustomMessageHandler('launch-modal', function(d) {$('#' + d).modal().focus();})"),
tags$script("Shiny.addCustomMessageHandler('remove-modal', function(d) {$('#' + d).modal('hide');})"),
tags$div(
  id = "my-modal",
  class="modal fade", tabindex="-1", `data-backdrop`="static", `data-keyboard`="false",
  tags$div(
    class="modal-dialog",
    tags$div(
      class = "modal-content",
      tags$div(class="modal-header", tags$h4(class="modal-title", "Running...")),
      tags$div(
        class="modal-body",
        shinyWidgets::progressBar(id = "pb", value = 0, display_pct = TRUE)
      )
    )
  )

服务器:

shinyWidgets::updateProgressBar(session = session, id = "pb", value = 0) # reinitialize to 0 if you run the calculation several times
  session$sendCustomMessage(type = 'launch-modal', "my-modal") # launch the modal
  withProgress({
    shinyWidgets::updateProgressBar(session = session, id = "pb", value = 99)
    heavyFunctionToExecute()
  })
  Sys.sleep(0.5)
  session$sendCustomMessage(type = 'remove-modal', "my-modal") # hide the modal programmatically

1 个答案:

答案 0 :(得分:-1)

您可以在value

中添加progressBar

如果您的重函数包含for loop,这可以解决问题:

withProgress(message = "Heavily working", value = 0, {
     for (i in 100){
         function do_something_heavy_100_times(x[i])
         incProgress(1/100))
         }
})