1

I have multiple tables in reactive objects in my shiny app (RA_s,Per), and I download each separately. Now, I'm trying to download one zip file containing all of this tables using a download button.

Here is my code, I don't know how to complete downloadHandler function. I want to download a zip file which contain 2 csv files RA_s and Per.

Code

shinyServer(function(input, output) {

  RA_s <- reactive({
    iris
  })

  Per <- reactive({
    sepal1 <- RA_s()["Sepal_Length"]
    sepal2 <- RA_s()["Sepal_Width"]
    value = sepal1*sepal2
    c = cbind(RA_s(),value)
  })

  output$downloadData <- downloadHandler(
  filename = function() {
    paste0("output", ".zip")
  },
  content = function(file) {
  ...
  })
})


sidebar <- dashboardSidebar(
  sidebarMenu(
    menuItem("Download", tabName = "d")
)
body<- dashboardBody(
  tabItems(
    tabItem(tabName = "d",
         downloadButton('downloadData', 'Download')
     )
  )
dashboardPage(
dashboardHeader(title = "Valo"),
sidebar,
body
)
a.lay
  • 161
  • 2
  • 8

1 Answers1

0

You can first save the zip files, and then zip them, as follows:

library(shiny)
server<- shinyServer(function(input, output) {

  RA_s <- reactive({
    iris
    print(iris)
  })

  Per <- reactive({
    sepal1 <- RA_s()["Sepal.Length"]
    sepal2 <- RA_s()["Sepal.Width"]
    value = sepal1*sepal2
    c = cbind(RA_s(),value)
  })

  output$downloadData <- downloadHandler(
    filename = 'two_csvs.zip',
    content = function(fname) {

      write.csv(RA_s(), file = "csv1.csv", sep =",")
      write.csv(Per(), file = "csv2.csv", sep =",")

      zip(zipfile=fname, files=c("csv1.csv","csv2.csv"))
    },
    contentType = "application/zip"
  )

})

ui<- shinyUI(

  downloadButton('downloadData', 'Download')

)

shinyApp(ui,server)

You can specify a subdirectory of course, to keep your directory clean. Hope this helps!

Florian
  • 24,425
  • 4
  • 49
  • 80
  • Thanks @Florian it works very well. How can i specify a directory in which i want to save my zip file ? – a.lay Jul 24 '17 at 11:14
  • You can specify the directory for your csv's by doing `write.csv(RA_s(), file = "dirname/csv1.csv", sep =",")`, where dirname should be the name of your directory. Please note that the directory has to exist for that to work! You can create a folder 'storage' in the same folder as your ui.R and server.R for example. Please consider accepting my answer if it was helpful, thanks! – Florian Jul 24 '17 at 11:21
  • I still have a little problem. In addition to the zip file, the csv files are also downloaded separatly in my working directory – a.lay Jul 24 '17 at 11:47
  • I think they are created because of the write.csv statements. Did you create an additional folder in your working directory and adjust the write csv statements as per my previous comments? – Florian Jul 24 '17 at 11:49
  • Yes but it doesn't work. i have this error : 'Error : cannot open the connection' – a.lay Jul 24 '17 at 11:53
  • There are many answers to that question on Stack Overflow. Probably you are trying to write to a non-existent directory. [maybe this helps?](https://stackoverflow.com/questions/35604351/error-cannot-open-the-connection-in-r-shiny) – Florian Jul 24 '17 at 13:09