Well I've looked all over, but cannot figure this one out!
Here is a minimal reprex for making 2 plots. A download button will allow either plot to be downloaded.
How can I download both plots in a zipped file?
library(shiny)
library(ggplot2)
runApp(list(
ui = fluidPage(downloadButton("downloadPlot"),
plotOutput("myplot1"),
plotOutput("myplot2")),
server = function(input, output) {
#A reactive that generates desired plots
#(based on user-input in real data, but simplified for this reprex)
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)
#Adapting examples found online to have these plots downloadable
.download_reactive <- reactive({
return(list(
p1=first_reactive()$p1,
p2=first_reactive()$p2
))
})
plotInput = function(){return(list(
p1=.download_reactive()$p1,
p2=.download_reactive()$p2
))}
#Download p1. This also would work to download p2.
#Looking for a way to download them both in a zipped file
output$downloadPlot <- downloadHandler(
filename="myplot1.svg",
content = function(file){
ggsave(file, plot=plotInput()$p1)
}
)
}
))