英文:
save multiple pdf pages with different sizes in Shiny R
问题
这是我目前有的代码:
output$exportall <- downloadHandler(
filename="Allplots.pdf",
content=function(file){
withProgress(message = 'Exporting', min=0, max=1, {
pdf(file, width=8, height=11)
print(plot1(width=7, height=7))
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()
})
}
)
英文:
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?
答案1
得分: 0
也许最简单的方法是创建单独的 PDF 文件(适当大小),然后使用 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) # 或其他内容
dev.off()
qpdf::pdf_combine(paste0(file, c(".8x11", ".7x7")), file)
file.remove(paste0(file, c(".8x11", ".7x7")))
生成的 file.pdf
页面:
如果您的尺寸不总是按顺序排列(例如,8x11、7x7、8x11),您可以选择以下两种方法之一:
- 创建三个 PDF 文件(需要调整文件名约定)并按顺序连接,或者
- 创建两个 PDF 文件(按尺寸),然后还可以使用
qpdf::pdf_subset
...不过由于这会创建新的 PDF 文件,您随后需要将它们包含在pdf_combine
中,这似乎不是最高效的方法。
我无法测试这个代码,但我认为这意味着您的代码应该是这样的:
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)
})
}
)
英文:
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:
- create three PDF files (would need an adjusted file name convention) and concatenate in order, or
- create two PDF files (by dimensions), then also use
qpdf::pdf_subset
... though since this creates new PDF files that you would then need to include inpdf_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)
})
}
)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论