Sourabh Potnis
Sourabh Potnis

Reputation: 1471

Shiny download file not working

I am creating R Shiny application in which I have a dropdown (selectInput) of documents list and a download button. So functionality is Users will select a document from the list and download that doc using download button.

in UI.R

  tabItem(tabName = "Downloads",
                selectInput("filenames", "Choose a document to download:",list.files(path="/srv/shiny-server/apps/dsw/files")),
  downloadButton('downloadData', 'Download')),

in Server.R

        datasetInput <- reactive({
switch(input$filenames,input$filenames)
   })

output$downloadData <- downloadHandler(
filename = function() { 
paste(input$filenames) 
},
content = function(file) {
write.csv(datasetInput(), file)
}
)

These documents I have placed on Linux server in the www folder of my shiny application.

myapp

--app.R
--files
   -- Doc1.doc
   -- Doc2.csv

When I run the application, it just downloads empty .csv or .docx file but not the actual file from server.

Upvotes: 0

Views: 4277

Answers (1)

Sourabh Potnis
Sourabh Potnis

Reputation: 1471

Solved it as follows:

UI.R

 tabItem(tabName = "Downloads",
            selectInput("filenames", "Choose a document to download:",list.files(path="/srv/shiny-server/apps/files")),
 downloadButton('downloadData', 'Download'))

Server.R

output$downloadData <- downloadHandler(
filename = function() {
paste(input$filenames, sep='')
},
content = function(file) {
myfile <- paste0('/srv/shiny-server/apps/files/',input$filenames, collapse = NULL)
  file.copy(myfile, file)
}
)

Upvotes: 1

Related Questions