Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading message for slow downloadhandler()

Tags:

r

shiny

I would like to ask if there is a way to display a loading message before the file is finally downloaded in my shiny app. My original dataset is big and I guess this is the reason for this delay. Below I attach a toy example in case someone can apply the reuested solution on this.

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

}
like image 933
firmo23 Avatar asked Dec 24 '22 01:12

firmo23


1 Answers

I have implemented a solution based on your code. What you need to do is add a progress bar inside your 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)

You can tailor this solution to your requirements (customize message, add loop etc). I hope this helps :-)

like image 146
RickTastic Avatar answered Dec 31 '22 16:12

RickTastic