Thanks for the link! I read up on it and several other examples, but it seems there aren't really examples that completely solve this (that I could find).
So I modified an example from the link you mentioned, but I can only get it to work when re-generating the plot completely. This would mean pasting the code within each download handler - but this isn't feasible because my real data has many plots and they're all complex.
How can I make this work but instead reference the plot rather than re-make it just to download it?
This works:
library(shiny)
library(ggplot2)
runApp(list(
ui = fluidPage(downloadButton("downloadPlot"),
plotOutput("myplot")),
server = function(input, output) {
output$myplot <- renderPlot({
myplot <- ggplot(mtcars, aes(x=cyl, y=mpg, color=mpg)) + geom_point()
myplot
})
plotInput = function() {
##################################################
##################################################
#This method works - but requires re-generating the plot for every download button!
ggplot(mtcars, aes(x=cyl, y=mpg, color=mpg)) + geom_point()
##################################################
##################################################
}
output$downloadPlot <- downloadHandler(
filename = function() { paste("test", '.svg', sep='') },
content = function(file) {
ggsave(file, plot = plotInput(), device = "svg")
}
)
}
))
...but this does not work by simply referencing the plot -- maybe I'm doing it wrong?
library(shiny)
library(ggplot2)
runApp(list(
ui = fluidPage(downloadButton("downloadPlot"),
plotOutput("myplot")),
server = function(input, output) {
output$myplot <- renderPlot({
myplot <- ggplot(mtcars, aes(x=cyl, y=mpg, color=mpg)) + geom_point()
myplot
})
plotInput = function() {
##################################################
##################################################
#DOES NOT WORK obviously - because 'myplot' is only defined inside of the 'renderPlot' from above
myplot
##################################################
##################################################
}
output$downloadPlot <- downloadHandler(
filename = function() { paste("test", '.svg', sep='') },
content = function(file) {
ggsave(file, plot = plotInput(), device = "svg")
}
)
}
))