将大型数据框导出为pdf文件

时间:2017-07-05 06:03:27

标签: r pdf

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

任何想法如何实现?

代码:

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

pdf(file = "df2.pdf")

grid.table(df)
dev.off()

2 个答案:

答案 0 :(得分:3)

@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()

希望这有效!

答案 1 :(得分:0)

@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()