代码之家  ›  专栏  ›  技术社区  ›  firmo23

正在为慢速下载处理程序()加载消息

  •  1
  • firmo23  · 技术社区  · 6 年前

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

    #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 回复  |  直到 6 年前
        1
  •  1
  •   RickTastic    6 年前

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

    您可以根据自己的需求定制这个解决方案(定制消息、添加循环等)。我希望这有帮助:—)