Im developing a shiny app with several features. I added a button to download a single pdf file that contains many plots. I want to save those plots in individual pages but I want to choose the size of each pdf page. Is that possible?
This is he code that have so far:
output$exportall<-downloadHandler(
filename="Allplots.pdf",
content=function(file){
withProgress(message = 'Exporting', min=0,max=1, {
pdf(file,width=8,height=11)
print(plot1())
print(histogram())
print(plots2())
print(marrangeGrob(woodsbytimepoint(), nrow=2, ncol=1))
print(digestion())
print(map())
print(marrangeGrob(allplots(), nrow=4, ncol=2, top=NULL))
dev.off()
})
}
)
The code works fine and exports all the plots that I want. However, all pages in the pdf file are 8x11. Is there a way to speciffy the size of each page? for example I want the first plot to be 7x7 and all other 8x11.
Any ideas?
Perhaps the simplest is to create separate PDFs (sized appropriately) and combine them with qpdf::pdf_combine
.
file <- "file.pdf"
pdf(paste0(file, ".8x11"), width=8, height=11)
plot(disp ~ mpg, data = mtcars)
gg <- ggplot(mtcars, aes(disp, mpg)) + geom_point()
print(gg)
dev.off()
pdf(paste0(file, ".7x7"), width=7, height=7)
print(gg) # or anything else
dev.off()
qpdf::pdf_combine(paste0(file, c(".8x11", ".7x7")), file)
file.remove(paste0(file, c(".8x11", ".7x7")))
The resulting file.pdf
pages:
If your sizes are not always in order (e.g., 8x11, 7x7, 8x11), you can either:
qpdf::pdf_subset
... though since this creates new PDF files that you would then need to include in pdf_combine
, it hardly seems the most efficient method.I cannot test this, but I think this means your code should be
output$exportall<-downloadHandler(
filename="Allplots.pdf",
content=function(file){
withProgress(message = 'Exporting', min=0,max=1, {
pdf(paste0(file, ".7x7"), width=7, height=7)
print(plot1())
dev.off()
pdf(paste0(file, ".8x11"), width=8, height=11)
print(histogram())
print(plots2())
print(marrangeGrob(woodsbytimepoint(), nrow=2, ncol=1))
print(digestion())
print(map())
print(marrangeGrob(allplots(), nrow=4, ncol=2, top=NULL))
dev.off()
qpdf::pdf_combine(paste0(file, c(".7x7", ".8x11")), output=file)
})
}
)