R Shiny for循环中的进度条

时间:2018-11-21 16:01:07

标签: r shiny

我无法让进度条与selectInput函数配合使用。它提供了用户选择加载到页面的数据的进度,但是没有提供执行该数据应做的循环的应用程序。这是我到目前为止的内容:

ui <- fluidPage(  
  titlePanel("Upload Files"),  
  sidebarLayout(  
    sidebarPanel(  
      fileInput("upload", "Select Files",  
                multiple = TRUE,  
                accept = c("text/csv",  
                           "text/comma-separated-values,text/plain"))  
    ),  
    mainPanel(  
      tableOutput("table")  
    )  
  )  
)  

server <- function(input, output) {  
  observeEvent(input$upload, {for (i in 1:length(input$upload$datapath)) {  *code to execute*  
}})  
output$table <- renderTable(  
print(input$upload$name))  
}  
shinyApp(ui,server)

1 个答案:

答案 0 :(得分:2)

我不确定是否可以调整fileInput的加载栏以响应循环,但这是带有新进度栏的替代方法。

ui <- fluidPage( 

  # With this tag you can change the position and the size of the progress bar
  # Without it, the bar will be at the bottom right corner of the screen
  tags$head(tags$style(".shiny-notification {position: fixed; 
                                             opacity: 1 ;
                       top: 35% ;
                       left: 40% ;
                       height: 50px;
                       width: 300px}")),

  titlePanel("Upload Files"),  
  sidebarLayout(  
    sidebarPanel(  
      fileInput("upload", "Select Files",  
                multiple = TRUE,  
                accept = c("text/csv",  
                           "text/comma-separated-values,text/plain"))  
    ),  
    mainPanel(  
      tableOutput("table")  
    )  
  )  
)  

server <- function(input, output) { 

  observeEvent(input$upload, {

    # wrap the loop execution in withProgress
    withProgress(
      message='Please wait',
      detail='Doing important stuff...',
      value=0, {

        for (i in 1:5) {  # changed the loop for demonstration purposes

          Sys.sleep(0.8)  # pretending to execute code
          incProgress(amount=1/5)
        }
      })
  })

  output$table <- renderTable(print(input$upload$name))  
}  

shinyApp(ui,server)