首页 > 解决方案 > 根据时间和进度消息自定义闪亮应用程序中的进度指示器

问题描述

我在下面有一个闪亮的应用程序,我想以如下方式自定义此进度指示器:前 10 秒将显示"Analysis running",接下来的 10 秒将显示"Still running",最后 10 秒显示" Almost there"。我知道这个情节创建得太快了,但我的原稿需要时间,我想将进度指示器设置为显示 30 秒。

server <- function(input, output) {
  output$plot <- renderPlot({
    input$goPlot # Re-run when button is clicked
    
    # Create 0-row data frame which will be used to store data
    dat <- data.frame(x = numeric(0), y = numeric(0))
    
    # Create a Progress object
    progress <- shiny::Progress$new()
    # Make sure it closes when we exit this reactive, even if there's an error
    on.exit(progress$close())
    
    progress$set(message = "Analysis running", value = 0)
    
    # Number of times we'll go through the loop
    n <- 10
    
    for (i in 1:n) {
      # Each time through the loop, add another row of data. This is
      # a stand-in for a long-running computation.
      dat <- rbind(dat, data.frame(x = rnorm(1), y = rnorm(1)))
      
      # Increment the progress bar, and update the detail text.
      progress$inc(1/n, detail = paste("Doing part", i))
      
      # Pause for 0.1 seconds to simulate a long computation.
      Sys.sleep(0.1)
    }
    
    plot(dat$x, dat$y)
  })
}

ui <- shinyUI(basicPage(
  plotOutput('plot', width = "300px", height = "300px"),
  actionButton('goPlot', 'Go plot')
))

shinyApp(ui = ui, server = server)

标签: rshiny

解决方案


推荐阅读