【问题标题】:Export large dataframe to a pdf file将大型数据框导出到 pdf 文件
【发布时间】:2017-07-05 06:03:26
【问题描述】:

我想将我的数据框导出为 pdf 文件。数据框非常大,因此在导出时会引起问题。我使用了此处指定的 gridExtra 包writing data frame to pdf table,但它不适用于我的数据框,因为它包含大量数据。

任何想法如何实现?

代码:

library(gridExtra)
df <- data.frame(replicate(10,sample(0:1,1000,rep=TRUE)))

pdf(file = "df2.pdf")

grid.table(df)
dev.off()

【问题讨论】:

  • 请提供“无效”的代码和示例数据框。
  • 你有什么错误吗?可以发在这里吗?
  • @zx8754 我已经更新了描述以包含代码和数据。它不会抛出任何错误,但它只是将数据框部分压缩到 pdf 的一页上,而不是将其扩展到所需的页数。
  • @Indi 请看我上面的评论

标签: r pdf


【解决方案1】:

@Baqir,您可以尝试此链接上给出的解决方案: https://thusithamabotuwana.wordpress.com/2016/01/02/creating-pdf-documents-with-rrstudio/

会是这样的:

    library(grid)    
    library(gridExtra)  
    df <- data.frame(replicate(10,sample(0:1,1000,rep=TRUE)))    
    dim(df)  
    maxrow = 35   
    npages = ceiling(nrow(df)/maxrow)      
    pdf("test.pdf", height = 11, width = 8.5)  
    idx = seq(1, maxrow)  
    grid.table(df[idx,],rows = NULL)  
    for(i in 2:npages){
      grid.newpage();
      if(i*maxrow <= nrow(df)){
      idx = seq(1+((i-1)*maxrow), i * maxrow)
    }
    else{
      idx = seq(1+((i-1)*maxrow), nrow(df))
    }
    grid.table(df[idx, ],rows = NULL)
    }
    dev.off()

希望这行得通!

【讨论】:

  • @ShiwaniVeer 如何整合在我的 pdf 中所有页面重复的标题和脚注?您的解决方案效果很好,但我需要标题和脚注才能浏览所有这些页面。
  • @Pryore 转到提供的链接。使用 sweave(本质上是乳胶)有一个更好的例子。所以乳胶代码应该可以工作。 overleaf.com/learn/latex/Headers_and_footers
【解决方案2】:

@Pryore,我从链接中找到了部分解决方案: link

这是页眉和页脚的代码。 希望这行得通!

      makeHeader <- function(headerText= "your header", size= 1, color= grey(.5))
        {
          require(grid)
          pushViewport(viewport())
          grid.text(label= headerText,
                    x = unit(1,"npc") - unit(110, "mm"),
                    y = unit(270.8, "mm"),
                    gp=gpar(cex= size, col=color))
          popViewport()
        }

      makeFootnote <- function(footnoteText= "your footnote", 
                                  size= 1, color= grey(.5))
        {
          require(grid)
          pushViewport(viewport())
          grid.text(label= footnoteText ,
                    x = unit(1,"npc") - unit(27, "mm"),
                    y = unit(3, "mm"),
                    gp=gpar(cex= size, col=color))
          popViewport()
        }

     library(grid)    
     library(gridExtra)
        df <- data.frame(replicate(10,sample(0:1,1000,rep=TRUE)))    
        dim(df)  
        maxrow = 35   
        npages = ceiling(nrow(df)/maxrow)      
        pdf("trial.pdf", height = 11, width = 8.5)  
        idx = seq(1, maxrow)  
        grid.table(df[idx,],rows = NULL)  
        for(i in 1:npages){
          grid.newpage();
        makeFootnote()
        makeHeader()
          if(i*maxrow <= nrow(df)){
          idx = seq(1+((i-1)*maxrow), i * maxrow)
        }
        else{
          idx = seq(1+((i-1)*maxrow), nrow(df))
        }
        grid.table(df[idx, ],rows = NULL)
        }
        dev.off()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-09-05
    • 2018-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-30
    • 1970-01-01
    • 2016-12-01
    相关资源
    最近更新 更多