【问题标题】:Loading message for slow downloadhandler()加载慢速下载处理程序()的消息
【发布时间】:2019-06-14 03:10:10
【问题描述】:

我想问是否有办法在文件最终下载到我的闪亮应用程序之前显示加载消息。我的原始数据集很大,我想这就是延迟的原因。下面我附上一个玩具示例,以防有人可以在此应用reueted解决方案。

#ui.r
ui <- fluidPage(

  # App title ----
  titlePanel("Downloading Data"),

  # Sidebar layout with input and output definitions ----
  sidebarLayout(

    # Sidebar panel for inputs ----
    sidebarPanel(

      # Input: Choose dataset ----
      selectInput("dataset", "Choose a dataset:",
                  choices = c("rock", "pressure", "cars")),

      # Button
      downloadButton("downloadData", "Download")

    ),

    # Main panel for displaying outputs ----
    mainPanel(

      tableOutput("table")

    )

  )
)
#server.r
server <- function(input, output) {

  # Reactive value for selected dataset ----
  datasetInput <- reactive({
    switch(input$dataset,
           "rock" = rock,
           "pressure" = pressure,
           "cars" = cars)
  })

  # Table of selected dataset ----
  output$table <- renderTable({
    datasetInput()
  })

  # Downloadable csv of selected dataset ----
  output$downloadData <- downloadHandler(
    filename = function() {
      paste(input$dataset, ".csv", sep = "")
    },
    content = function(file) {
      write.csv(datasetInput(), file, row.names = FALSE)
    }
  )

}

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    我已经根据您的代码实施了一个解决方案。您需要做的是在您的downloadhandler() 中添加一个进度条。

    library(shiny)
    ui <- fluidPage(
    
    
    
    # App title ----
      titlePanel("Downloading Data"),
    
      # Sidebar layout with input and output definitions ----
      sidebarLayout(
    
        # Sidebar panel for inputs ----
        sidebarPanel(
    
          # Input: Choose dataset ----
          selectInput("dataset", "Choose a dataset:",
                      choices = c("rock", "pressure", "cars")),
    
          # Button
          downloadButton("downloadData", "Download")
    
        ),
    
        # Main panel for displaying outputs ----
        mainPanel(
    
          tableOutput("table")
    
        )
    
      )
    )
    #server.r
    server <- function(input, output) {
    
      # Reactive value for selected dataset ----
      datasetInput <- reactive({
        switch(input$dataset,
               "rock" = rock,
               "pressure" = pressure,
               "cars" = cars)
      })
    
      # Table of selected dataset ----
      output$table <- renderTable({
        datasetInput()
      })
    
      # Downloadable csv of selected dataset ----
      output$downloadData <- downloadHandler(
        filename = function() {
          paste(input$dataset, ".csv", sep = "")
        },
        content = function(file) {
          shiny::withProgress(
            message = paste0("Downloading", input$dataset, " Data"),
            value = 0,
            {
              shiny::incProgress(1/10)
              Sys.sleep(1)
              shiny::incProgress(5/10)
              write.csv(datasetInput(), file, row.names = FALSE)
            }
          )
        }
      )
    
    }
    shiny::shinyApp(ui = ui, server = server)
    

    您可以根据您的要求定制此解决方案(自定义消息、添加循环等)。我希望这会有所帮助:-)

    【讨论】:

    • 肯定是个好答案
    • 你好,加载时间需要先预估一下,可以定制吗?比如大数据集我们设置10s,小数据集我们设置1s?
    • 我只是注意到,当您按下按钮时,下载栏并没有出现。它仍然需要等到数据被检索到然后显示bar,并且bar的处理时间并没有反映数据检索的实际时间
    猜你喜欢
    • 2017-05-31
    • 1970-01-01
    • 2022-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-06
    • 2016-12-18
    • 1970-01-01
    相关资源
    最近更新 更多