正在加载有关缓慢的downloadhandler()的消息

时间:2019-01-20 15:22:25

标签: r shiny

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

#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)
    }
  )

}

1 个答案:

答案 0 :(得分:2)

我已经根据您的代码实施了一个解决方案。您需要做的是在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)

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