0

As I continue to work on my first shiny dashboard, I have been struggling with a task. I have a folder (with datasets) in my dropbox, and I want to be able to download a file from it as follows:

  1. a user chooses a file via selectInput()
  2. then he/she clicks the downloadButton() to save the file to a local machine.

So far, I only got to save a file (.html) that partially reproduces the app, and not the data file. I have tried different approaches (from what I have learned on the web) with no success. It follows the relevant pieces of my code. Thank you for any help!

Global

Get File Names from folder in dropbox

filenames <- function(){
 drop_dir('Partners Files') %>%
 pull()
}

UI piece

# To download  a file 

selectInput("dataset", "Choose a Dataset", choices = filenames()),
tableOutput("preview"),
downloadButton("download", "Download .csv"),

Server piece

To download a file

data_down <- reactive({
    req(input$dataset())
  })

When the Download a File button is clicked, save the data

observeEvent(input$download, {
    drop_download(data_down())
  })        
  • Is the file public? That is, can you skip the step of downloading the file to the shiny server first, then having the user download it from there and just have the user download it from dropbox directly? Something like this would work in that case... https://stackoverflow.com/questions/37795760/r-shiny-add-weblink-to-actionbutton – cory May 27 '20 at 14:33
  • Thank you! No, the file is not public. But I have already set up a connection between my app and dropbox. In a previous step, the dashboard allows users to upload files to dropbox. Now, I want them to be able to download the files to their local machines. – Emerson De Maria May 27 '20 at 14:40
  • Looks like you are missing the `downloadHandler` part of this. https://shiny.rstudio.com/articles/download.html – cory May 27 '20 at 19:16
  • Thanks, Cory. Actually, I have already added this piece (newer version). The thing now is that I can't find a way to pass the user's input from input$dataset to drop_download function. – Emerson De Maria May 27 '20 at 19:31
  • doesn't `drop_download(input$dataset)` work? – cory May 27 '20 at 19:38
  • No, I got the error: Warning: Error in drop_download: Conflict (HTTP 409). [No stack trace available] – Emerson De Maria May 27 '20 at 19:52

1 Answers1

0

After several trial and error attempts, I could get this done. I am very new to this stuff, but it was kind of fun. I am posting my solution if anyone else ever faces an issue like this one.

To download a file:

#Downloadable csv of selected dataset
  output$downloadData <- downloadHandler(
    filename = function() {
      paste(input$dataset, sep = "")
    },
    content = function(file) {
      drop_download(paste0("Partners Files/", input$dataset), overwrite = T)
    }
  )

I guess that one of the mistakes I did was trying to use a reactive variable for the input$dataset. One issue remains: my app allows users to upload a file to dropbox; then what I did (in this piece here) was to make that file available for other users to download it to their local machine. However, I noticed that the choices of files available via the select$Input() (for downloading) only become available after reloading the app. Is there a way to fix this? I think it's related to the reactive or observeEvent piece. I hope this helps people, and I may get some help with this other piece.