r - 为慢速下载处理程序()加载消息

标签 r shiny

我想问一下,是否有办法在文件最终下载到我 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)
    }
  )

}

最佳答案

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

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

关于r - 为慢速下载处理程序()加载消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54277922/

相关文章:

r - R中的coplot-如何分辨哪个图是哪个

r - 在 Shiny 的 react 结构中子集数据帧

r - 如果 Rmd 文件未更改,则页面不会呈现

R Shiny : Write looped reactive inputs to textfile

r - 使用 YAML 设置参数 IN YAML header Knit 和参数

r - 缓慢的 data.frame 行分配

r - 每行第一个字加上双引号

将列中的字符串替换为另一个数据帧 R 中另一列中的等效字符串

html - 如何在 Shiny 的仪表板中添加除元素名称之外的 Logo ?

r - 根据输入 Shiny 的 iframe