【问题标题】:saving multiple plots in a single PDF in Shiny R在 Shiny R 中将多个绘图保存在单个 PDF 中
【发布时间】:2018-08-20 02:59:46
【问题描述】:

我正在尝试使用下载处理程序将我的 Shiny App 中的 ggplots 导出到单个 PDF 文件中,但它不起作用。 PDF 文件正在保存,但它只给了我最后一个 ggplot 而不是全部三个。任何帮助将不胜感激!

下面是服务器的代码:

shinyServer(function(input, output, session) {
plotinput()
{
df<-data.frame(q=c(1,3,5,7,9),w=c(2,4,6,8,10),z=c(1,2,3,4,5))
ggplot(df,aes(x=q,y=w))+geom_point()
ggplot(df,aes(x=z,y=w))+geom_point()
ggplot(df,aes(x=q,y=z))+geom_point()
}
output$allgraphs <- downloadHandler(
filename = function(){paste0("graphs.pdf")},
content = function(file){
pdf(file,onefile = TRUE)
print(plotinput())
dev.off()
}
) 
})

【问题讨论】:

    标签: r pdf ggplot2 download shiny


    【解决方案1】:

    我们可以这样做

    library(shiny)
    library(grid)
    library(gridExtra)
    
    
    
    runApp(list(
      ui = fluidPage(downloadButton('allgraphs')),
      server = function(input, output) {
    
        plotinput <- function() {
          df<-data.frame(q=c(1,3,5,7,9),w=c(2,4,6,8,10),z=c(1,2,3,4,5))
          list(p1 = ggplot(df,aes(x=q,y=w))+geom_point(),
          p2 =  ggplot(df,aes(x=z,y=w))+geom_point(),
          p3 = ggplot(df,aes(x=q,y=z))+geom_point())
        }
    
        output$allgraphs = downloadHandler(
          filename = 'graphs.pdf',
          content = function(file) {
           pdf(file)
    
            arrangeGrob(print(plotinput()[['p1']]),
                        print(plotinput()[['p2']]), 
                        print(plotinput()[['p3']]), ncol = 3)  
            dev.off()
          })
      }
    ))
    

    -输出

    allgraphs.pdf

    1

    2

    3

    【讨论】:

      猜你喜欢
      • 2020-11-30
      • 2012-07-04
      • 2018-11-29
      • 1970-01-01
      • 2014-08-03
      • 1970-01-01
      • 2023-03-11
      • 1970-01-01
      相关资源
      最近更新 更多