Skip to content

Instantly share code, notes, and snippets.

@aagarw30
Last active April 17, 2017 18:04
Show Gist options
  • Save aagarw30/bde12c6f2e3de6095b4b2858d6e41802 to your computer and use it in GitHub Desktop.
Save aagarw30/bde12c6f2e3de6095b4b2858d6e41802 to your computer and use it in GitHub Desktop.
Download multi-plots in PDF
library(shiny)
library(ggplot2)
shinyServer(function(input,output)({
# downloadHandler contains 2 arguments as functions, namely filename, content
output$down <- downloadHandler(
filename = function() {
paste("multiplot.pdf")
},
# content is a function with argument file. content writes the plot to the device
content = function(file) {
pdf(file) # open the pdf device
## Plot multiple using par for multi-plots
par(mfrow=c(1,2))
print(ggplot(mtcars, aes(x=wt, y=mpg)) + geom_point() + labs("mtcars plot1"))
print(ggplot(mtcars, aes(x=factor(am), y=mpg)) + geom_boxplot() + labs("mtcars plot2"))
## Uncomment the below if you want to experiment with base plot system
# plot(mtcars$mpg, mtcars$cyl)
# plot(mtcars$cyl, mtcars$hp)
dev.off() # turn the device off
}
)
}))
library(shiny)
shinyUI(fluidPage(
titlePanel("Download PDF with multi plots in Shiny - an example"),
sidebarLayout(
sidebarPanel(
downloadButton(outputId = "down", label = "Download the plot")
),
mainPanel()
)
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment