【问题标题】:Adjust size of Shiny progress bar and center it调整闪亮进度条的大小并将其居中
【发布时间】:2017-10-18 00:22:10
【问题描述】:

我正在使用一个 Shiny 应用程序,我需要在其中计算进程,并且在执行计算进度时,我使用 progressBar 来显示进程。

问题是进度条太小了,显示的方式我不喜欢。

所以,我在想也许有一种方法可以使用 Shiny 模态来实现进度条(有一个名为 modalDialog 的函数)。

我的想法是,当用户运行计算时,将打开一个显示progressBar 的模式。

这是进度代码:

withProgress(message = 'Runing GSVA', value = 0, {
    incProgress(1, detail = "This may take a while...")
    functionToGenerate()
  })

有什么想法吗?

【问题讨论】:

  • 您是否仅限于 modalDialog 或者预期输出的进度条大于当前进度条,例如居中?
  • 我不限于modalDialog,我只是希望当前进度条至少更大或居中,但使用模态也很酷。

标签: css r modal-dialog shiny progress-bar


【解决方案1】:

我建议自定义通知的 CSS 类: 如果您检查通知器的元素,您会发现它具有“闪亮通知”类。因此,您可以使用tags$style() 覆盖该类的某些属性。在下面的示例中(对于模板:请参阅?withProgress),我决定调整 height+width 以使其更大,top+left 使其居中。

ui <- fluidPage(
  tags$head(
    tags$style(
      HTML(".shiny-notification {
              height: 100px;
              width: 800px;
              position:fixed;
              top: calc(50% - 50px);;
              left: calc(50% - 400px);;
            }
           "
      )
    )
  ),
  plotOutput("plot")
)

server <- function(input, output) {
  output$plot <- renderPlot({
    withProgress(message = 'Calculation in progress',
                 detail = 'This may take a while...', value = 0, {
                   for (i in 1:15) {
                     incProgress(1/15)
                     Sys.sleep(0.25)
                   }
                 })
    plot(cars)
  })
}

runApp(shinyApp(ui, server), launch.browser = TRUE)

【讨论】:

  • 这样比较好,但是如果progressBar在屏幕中间,app仍然显示主要数据,progressBar会与数据重叠,结果不可读。这就是为什么我想使用modalDialog 来隐藏主要数据并只显示progressBar
  • 这就是我在评论中询问规格的原因;)所以具体来说,浅灰色背景的透明度令人不安?
  • 我知道,非常感谢您的帮助。不,不是透明度,问题是主页面有很多数据,当显示'progressBar'时,它几乎不可见,因为页面中的所有元素,如果你把它放在中间问题是一样的。最好显示“progressBar”并模糊页面内容。
  • 好的,我认为 Victorps 解决方案已经涵盖了它 :) 但是如果您仍然对调整此解决方案感兴趣,请告诉我。
  • 是的,知道了。非常感谢你们。
【解决方案2】:

您好,我在 shinyWidgets 包中编写了一个进度条功能,您可以将其放在模态中,但与 shiny::showModal 一起使用会很棘手,因此您可以像下面这样手动创建自己的模态。需要编写更多代码,但运行良好。

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)

【讨论】:

  • 我喜欢完成后的模态删除
  • 感谢@Victorp 的这篇文章,我正在尝试做类似的事情。在我的应用程序中,进度条下方有一个selectInput,我想在循环的每次迭代中与进度条同步更新。但是,updateSelectInput 在循环内不起作用。你对为什么有任何见解吗?这是我的帖子:stackoverflow.com/questions/59799641/…。非常感谢您的意见。
  • PS 我也把它贴在RStudio Community
猜你喜欢
  • 2015-11-16
  • 1970-01-01
  • 1970-01-01
  • 2014-10-11
  • 1970-01-01
  • 2012-09-23
  • 1970-01-01
  • 1970-01-01
  • 2013-08-27
相关资源
最近更新 更多