在Shiny R中保存具有不同尺寸的多个PDF页面。

huangapple go评论67阅读模式
英文:

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&lt;-downloadHandler(
filename=&quot;Allplots.pdf&quot;,
content=function(file){
  withProgress(message = &#39;Exporting&#39;, 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 页面:

在Shiny R中保存具有不同尺寸的多个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 &lt;- &quot;file.pdf&quot;
pdf(paste0(file, &quot;.8x11&quot;), width=8, height=11)
plot(disp ~ mpg, data = mtcars)
gg &lt;- ggplot(mtcars, aes(disp, mpg)) + geom_point()
print(gg)
dev.off()
pdf(paste0(file, &quot;.7x7&quot;), width=7, height=7)
print(gg) # or anything else
dev.off()
qpdf::pdf_combine(paste0(file, c(&quot;.8x11&quot;, &quot;.7x7&quot;)), file)
file.remove(paste0(file, c(&quot;.8x11&quot;, &quot;.7x7&quot;)))

The resulting file.pdf pages:

在Shiny R中保存具有不同尺寸的多个PDF页面。

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 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&lt;-downloadHandler(
filename=&quot;Allplots.pdf&quot;,
content=function(file){
  withProgress(message = &#39;Exporting&#39;, min=0,max=1, { 
    pdf(paste0(file, &quot;.7x7&quot;), width=7, height=7)
    print(plot1())
    dev.off()
    pdf(paste0(file, &quot;.8x11&quot;), 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(&quot;.7x7&quot;, &quot;.8x11&quot;)), output=file)
  })
 }
)

huangapple
  • 本文由 发表于 2023年6月1日 20:30:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76381890.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定