简体   繁体   中英

Export large dataframe to a pdf file

I want to export my dataframe to a pdf file. Dataframe is pretty large, so it is causing problems while exporting. I used gridExtra package as specified here writing data frame to pdf table but it did not work for my dataframe as it contains a lot of data.

Any ideas how it can be achieved?

Code:

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

pdf(file = "df2.pdf")

grid.table(df)
dev.off()

@Baqir, you can try solution given on this link: https://thusithamabotuwana.wordpress.com/2016/01/02/creating-pdf-documents-with-rrstudio/

It will be like this:

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

Hope this works!

@Pryore, I found some part of the solution from the link: link

Here is the code for header and footer. Hope this works!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM