首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >调整发亮的进度条的大小并使其中心。

调整发亮的进度条的大小并使其中心。
EN

Stack Overflow用户
提问于 2017-05-18 09:13:19
回答 2查看 5.3K关注 0票数 5

我正在使用一个闪亮的应用程序,在这里我需要计算进程,当calc进程正在执行时,我使用progressBar来显示进程。

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

因此,我在想,也许有一种方法可以使用一个闪亮的模式来实现进度条(有一个叫做modalDialog的函数)。

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

这是进度代码:

代码语言:javascript
复制
withProgress(message = 'Runing GSVA', value = 0, {
    incProgress(1, detail = "This may take a while...")
    functionToGenerate()
  })

有什么想法吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-05-18 10:11:49

嗨,我在包shinyWidgets中写了一个进度条函数,你可以把它放在一个模态中,但是与shiny::showModal一起使用它很棘手,所以您可以像下面这样手动创建您自己的模式。这是更多的代码编写,但它的工作良好。

代码语言:javascript
复制
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)
票数 5
EN

Stack Overflow用户

发布于 2017-05-18 09:37:40

我建议自定义通知的CSS类:如果检查通知程序的元素,就会发现它有类“闪亮-通知”。因此,您可以用tags$style()覆盖该类的一些属性。在下面的示例中(关于模板:参见?withProgress),我决定调整height+width以使其更大,并将top+left调整为中心。

代码语言:javascript
复制
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)
票数 15
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44043475

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档