R Shiny中的进度条在事件中的活动呼叫

时间:2015-09-08 12:57:08

标签: r progress-bar shiny shiny-server

我在我的Shiny应用程序中使用eventReactive调用进行计算,从10秒到10000秒不等。我想向我的用户展示我们在循环中的距离。我无法使用闪亮提供的example,因为我需要存储来自eventReactive调用的几个对象。我提供了一个简化的示例,其中在R控制台上生成了一种“进度条”,但我不知道如何在Shiny中获得类似的东西。请注意,我需要在稍后的图中使用dat1,dat2和dat3。代码:

# This function computes a new data set. It can optionally take a function,
# updateProgress, which will be called as each row of data is added.
compute_data <- function(updateProgress = NULL) {
  # Create 0-row data frame which will be used to store data
  dat <- data.frame(x = numeric(0), y = numeric(0))

  for (i in 1:10) {
    Sys.sleep(0.25)

    # Compute new row of data
    new_row <- data.frame(x = rnorm(1), y = rnorm(1))

    # If we were passed a progress update function, call it
    if (is.function(updateProgress)) {
      text <- paste0("x:", round(new_row$x, 2), " y:", round(new_row$y, 2))
      updateProgress(detail = text)
    }

    # Add the new row of data
    dat <- rbind(dat, new_row)
  }

  dat
}


server <- function(input, output) {
  output$table <- renderTable({
    input$goTable

    # Create a Progress object
    progress <- shiny::Progress$new()
    progress$set(message = "Computing data", value = 0)
    # Close the progress when this reactive exits (even if there's an error)
    on.exit(progress$close())

    # Create a callback function to update progress.
    # Each time this is called:
    # - If `value` is NULL, it will move the progress bar 1/5 of the remaining
    #   distance. If non-NULL, it will set the progress to that value.
    # - It also accepts optional detail text.
    updateProgress <- function(value = NULL, detail = NULL) {
      if (is.null(value)) {
        value <- progress$getValue()
        value <- value + (progress$getMax() - value) / 5
      }
      progress$set(value = value, detail = detail)
    }

    # Compute the new data, and pass in the updateProgress function so
    # that it can update the progress indicator.
    compute_data(updateProgress)
  })


  Test <- eventReactive(input$goTable,{
    n <- 10
    dat1 <- data.frame(x = numeric(0), y = numeric(0))
    dat2 <- data.frame(x = numeric(0), y = numeric(0))
    dat3 <- data.frame(x = numeric(0), y = numeric(0))
    for (i in 1:n) {
      print(paste0(i*10,"%"))
      # Each time through the loop, add another row of data. This is
      # a stand-in for a long-running computation.
      dat1 <- rbind(dat1, data.frame(x = rnorm(1), y = rnorm(1)))
      dat2 <- rbind(dat2, data.frame(x = rnorm(1), y = rnorm(1)))
      dat3 <- rbind(dat3, data.frame(x = rnorm(1), y = rnorm(1)))


      # Increment the progress bar, and update the detail text.
      #incProgress(1/n, detail = paste("Doing part", i))

      # Pause for 0.1 seconds to simulate a long computation.
      Sys.sleep(0.5)

    }
    list(dat1,dat2,dat3)

  }) 

  output$table1 <- renderTable({Test()[[1]]})
  output$table2 <- renderTable({Test()[[2]]})
  output$table3 <- renderTable({Test()[[3]]})




}

ui <- shinyUI(basicPage(
  #tableOutput('table1'),
  actionButton('goTable', 'Start Computing..'),
  tableOutput('table1'),
  tableOutput('table2'),
  tableOutput('table3')
))

shinyApp(ui = ui, server = server)

非常感谢任何帮助。

0 个答案:

没有答案
相关问题