在闪亮的应用程序中以模式生成进度条,该进度条会自动关闭

时间:2018-09-30 02:06:11

标签: r shiny modal-dialog progress-bar

我正在使用需要大量时间进行计算的闪亮应用程序,我希望有一个模态进度条,该进度条会在所有计算工作后立即自动关闭。

理想的解决方案将具有两个功能

  1. 覆盖屏幕的大部分内容并阻止用户与应用互动
  2. 完成计算后自动关闭

我在以下问题中找到了解决方案:

library("shiny")
library("shinyWidgets")

ui <- fluidPage(
  actionButton(inputId = "go", label = "Launch long calculation"), #, onclick = "$('#my-modal').modal().focus();"

  # You can open the modal server-side, you have to put this in the ui :
  tags$script("Shiny.addCustomMessageHandler('launch-modal', function(d) {$('#' + d).modal().focus();})"),
  tags$script("Shiny.addCustomMessageHandler('remove-modal', function(d) {$('#' + d).modal('hide');})"),

  # Code for creating a modal
  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", "Calculation in progress")),
    tags$div(
      class="modal-body",
      shinyWidgets::progressBar(id = "pb", value = 0, display_pct = TRUE)
    ),
    tags$div(class="modal-footer", tags$button(type="button", class="btn btn-default", `data-dismiss`="modal", "Dismiss"))
  )
)
  )
)

server <- function(input, output, session) {

   value <- reactiveVal(0)

  observeEvent(input$go, {
    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
    # run calculation
    for (i in 1:10) {
      Sys.sleep(0.5)
      newValue <- value() + 1
      value(newValue)
      shinyWidgets::updateProgressBar(session = session, id = "pb", value = 100/10*i)
}
    Sys.sleep(0.5)
    # session$sendCustomMessage(type = 'remove-modal', "my-modal") # hide the modal programmatically
  })

}

shinyApp(ui = ui, server = server)

这解决了问题1,但我必须单击关闭以查看结果

2 个答案:

答案 0 :(得分:2)

progressbar中提供的原始shiny正是您所需要的。

但是我使用css使progessbar显示在屏幕中间。

您可以在闪亮的here中找到使用进度条的详细信息。

library("shiny")

ui <- fluidPage(
  actionButton(inputId = "go", label = "Launch long calculation"), #, onclick = "$('#my-modal').modal().focus();"

  # css to center the progress bar
  tags$head(
    tags$style(
      HTML(".shiny-notification {
           height: 100px;
           width: 800px;
           position:fixed;
           top: calc(50% - 50px);
           left: calc(50% - 400px);
           font-size: 250%;
           text-align: center;
           }
           "
      )
    )
  )
)

server <- function(input, output, session) {

  value <- reactiveVal(0)

  observeEvent(input$go, {
    withProgress(message = 'Calculation in progress', value = 0,detail="0%", {
      # run calculation
      for (i in 1:10) {
        Sys.sleep(0.5)
        newValue <- value() + 1
        value(newValue)
        incProgress(1/10,detail = paste0(i*10,"%"))
      }
      Sys.sleep(0.5)
    })
  })

}

shinyApp(ui = ui, server = server)

答案 1 :(得分:1)

不是一个完整的答案,只是回答其他CSS请求。您可以将css更改为,这将使面板填满整个页面。

.shiny-notification {
  height: 100%;
  width: 100%; 
  top: 0;
  left: 0;
  position:fixed;
  font-size: 250%;
  text-align: center;
  background-color: rgba(0, 0, 0, 0.7);
  color: white;
}