0

I'm trying to donwload an excel file already existing in my shiny app.

I've tried following code in server.R

   output$downloadbutton <- downloadHandler(

     filename <- function() {
       paste("result_balance", "xlsx", sep=".")
     },
     content <- function(file) {
       #temp <- file.path(tempdir(), "report.Rmd")
       file.copy(file.path(getwd(),'www','result.xlsx'), file,overwrite = TRUE)
     },
     contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
   )

but when user click on Download button it appears a download pop up like this:

enter image description here

and it will download a folder with xml/html files in it. Weird is that the proposed name is downloadbutton like my id shiny element.

I'm already aware of this R Shiny: Download existing file.

R version 3.6.2 on Ubuntu 18.04

Marco Fumagalli
  • 2,307
  • 3
  • 23
  • 41

2 Answers2

1

filename and content are arguments of the function downloadHandler, thus you have to use = and not <-:

 output$downloadbutton <- downloadHandler(

     filename = function() {
       paste("result_balance", "xlsx", sep=".")
     },
     content = function(file) {
       #temp <- file.path(tempdir(), "report.Rmd")
       file.copy(file.path(getwd(),'www','result.xlsx'), file,overwrite = TRUE)
     },
     contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
   )
Stéphane Laurent
  • 75,186
  • 15
  • 119
  • 225
  • Thanks I didn't notice. But the weird things is that in windows download is ok with the code I posted, in Linux I got the problem described above. – Marco Fumagalli Feb 17 '20 at 10:02
0

Tested on windows and linux ( Ubuntu) this function works:

 output$downloadbutton <- downloadHandler(

     filename = function() {
       paste("result_balance", "xlsx", sep=".")
     },
     content = function(file) {

       file.copy(file.path(getwd(),'www','result.xlsx'), file,overwrite = TRUE)
     },
     contentType = "text/xlsx"
   )
Marco Fumagalli
  • 2,307
  • 3
  • 23
  • 41