user3245256
user3245256

Reputation: 1968

r shiny: allow user to download shiny output in a desired directory

In my Shiny app, I run calculations, generate a data frame, and want the user to be able to save (download) it.

My relevant code in ui.R:

textInput("downFile","Save the File:", value = "Results File"),
downloadButton('downFile',"Save File"),

My relevant code in server.R:

output$downFile <- downloadHandler(
  filename = function() {
    paste0(input$downFile, " ", Sys.Date(), ".csv") 
  },
  content = function(file) {
      write.csv(MyMainFunction()$mydataframe, file, row.names = FALSE)
  }
)

Everything works. But I have two issues:

  1. If by mistake the user clicks on "Save File" before all the calculations in server.R have been completed, he gets an error ("Failed - Network Error").
  2. After all the calculations, when the user clicks on the "Save File" button, the file is immediately saved in "Downloads" folder.

Two questions:

  1. Is it possible to make the button invisible until the calculations in my main function (MainFunction) are completed?

  2. How could I allow the user to select a desired folder to download the results to? (as in "Save in...")

Thank you very much!

Upvotes: 2

Views: 1267

Answers (1)

Winicius Sabino
Winicius Sabino

Reputation: 1506

for question 1: Use shinyjs package learn more in https://deanattali.com/shinyjs/

  library(shinyjs)
   disable("downFile") # or use hide() Before all calculations 
   enable("downFile") # or use show() After 

For question 2: I think this is more about setting up your browser than the shiny

If you are using chrome go to the advanced settings and enable

enter image description here

Upvotes: 2

Related Questions