调整闪亮进度条的大小并使其居中

JFe*_*dez 3 css r modal-dialog progress-bar shiny

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

问题是进度条太小了,我不喜欢这种方式.

所以,我在想,也许有一种方法可以使用Shiny模式实现一个进度条(有一个叫做的函数modalDialog).

我的想法是,当用户运行calc时,将打开一个模态,显示一个progressBar.

这是进度代码:

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

任何的想法?

Big*_*ist 10

我建议自定义通知的CSS类:如果你检查通知程序的元素,你会看到它有类"shiny-notification".因此,您可以使用覆盖该类的某些属性tags$style().在下面的示例中(对于模板:请参阅?withProgress),我决定调整高度+宽度使其更大,顶部+左侧使其居中.

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)
Run Code Online (Sandbox Code Playgroud)

  • 好的,我认为 Victorps 解决方案已经涵盖了它:) 但是如果您仍然对调整此解决方案感兴趣,请告诉我。 (2认同)

Vic*_*orp 5

嗨,我在包中写了一个进度条函数shinyWidgets,你可以把它放在一个模态中,但是使用 with 很棘手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)
Run Code Online (Sandbox Code Playgroud)