Last active
April 17, 2017 18:04
-
-
Save aagarw30/bde12c6f2e3de6095b4b2858d6e41802 to your computer and use it in GitHub Desktop.
Download multi-plots in PDF
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
) | |
})) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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