在R服务器中运行下载按钮时显示“正在加载”,完成任务时则显示隐藏消息

时间:2020-06-30 21:30:17

标签: r shiny shinyapps

我正在尝试创建一个闪亮的应用程序,该应用程序可以从数据库中下载文件,但是查询时间很长,因此我想让用户知道,在按下按钮后,Web浏览器会显示“正在向用户加载” ”,下载完成后,“正在加载”将被隐藏。

这是我的代码:

if (interactive()) {
  
  library(shiny)
  library(shinyWidgets)
  
  ui <- fluidPage(
    downloadBttn(
      outputId = "downloadData",
      style = "bordered",
      color = "primary"
    )
  )
  
  server <- function(input, output, session) {
    data_xi <- data.frame(s = c(1:3),r = c(4:6), x =c(19:21))
    
    observeEvent(input$downloadData, {
      showModal(modalDialog("Loading", footer=NULL))
      
      filename = function() {
        paste('data-', Sys.Date(), '.csv', sep='')
      }
      
      content = function(file) {
        write_xlsx(data_xi,file)
      }
      
      removeModal()
    })
  }
  shinyApp(ui, server)
  
}

1 个答案:

答案 0 :(得分:0)

也许是这样的:

library(shiny)

ui <- fluidPage(
  downloadBttn(
    outputId = "downloadData",
    style = "bordered",
    color = "primary"
  )
)

server <- function(input, output, session) {
  data_xi <- data.frame(s = c(1:3),r = c(4:6), x =c(19:21))
  
  output$downloadData <- downloadHandler(
    filename = function() {
      paste('data-', Sys.Date(), '.csv', sep='')
    },
    content = function(file) {
      showModal(modalDialog("Loading", footer=NULL))
      on.exit(removeModal())
      write.csv(data_xi,file)
    }
  )
}

shinyApp(ui,server)
相关问题