downloadHandler does not work from shinyapps.io (but does work in RStudio).

My application runs smoothly in Rstudio, both in the viewer and also the browser (locally, using Chrome). Specifically, the .zip file downloads as expected.

When the app is deployed to shinyapps.io however, the file does not download (also using Chrome). A blank .html file appears with "Failed - Server problem" :

image

I can reproduce this with a minimal example that works in RStudio, but does not work deployed on shinyapps.io

library(shiny)
library(ggplot2)
library(zip)

zip_content <- function(file, plot1, plot2) {
  path_plot1 <- file.path(tempdir(), "plot1.svg")
  path_plot2 <- file.path(tempdir(), "plot2.svg")
  
  ggsave(path_plot1, plot1)
  ggsave(path_plot2, plot2)
  
  zip(
    zipfile = file, files = c(path_plot1, path_plot2), root = tempdir(), 
    mode = "cherry-pick"
  )
}


  ui <- fluidPage(downloadButton("downloadPlot"),
                 plotOutput("myplot1"),
                 plotOutput("myplot2"))
  
  server <-  function(input, output) {
    
    
    first_reactive <- reactive({
      p1 <- ggplot(mtcars, aes(x=mpg, y=wt))+geom_point(color="red",size=3)
      p2 <- ggplot(mtcars, aes(x=mpg, y=wt))+geom_point(color="blue",size=3)
      
      return(list(
        p1=p1,
        p2=p2
      ))
    })
    
    
    output$myplot1 <- renderPlot(first_reactive()$p1)
    output$myplot2 <- renderPlot(first_reactive()$p2)
    
    #Download both plots in a .zip file
    output$downloadPlot <- downloadHandler(
      filename="myplots.zip", 
      content = function(file){
        zip_content(file, first_reactive()$p1, first_reactive()$p2)
      },
      contentType = "application/zip"
    )
  }
  
  shinyApp(ui,server)

But when deployed to shinyapps.io it doesn't work :frowning:

This was solved by including library(svglite), which I found was the error after checking the shinyapps.io log for the app which had the line

Warning: Error in loadNamespace: there is no package called ‘svglite’

Not sure why this wasn't caught in the building: Installing packages stage of deployment.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.