1

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
  • 13
  • 1
  • 4
  • 1
    Please provide code and example dataframe that "did not work". – zx8754 Jul 05 '17 at 06:13
  • Did you get any error ? Can you post it here. – Indi Jul 05 '17 at 06:29
  • 1
    @zx8754 I have updated the description to include code and data. It does not throw any error, but it just squeeze the dataframe partially on one page in the pdf rather than expanding it to the required number of pages. – Baqir Jul 05 '17 at 13:09
  • @Indi plz see my above comment – Baqir Jul 05 '17 at 13:11

2 Answers2

6

@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!

  • @ShiwaniVeer how can I integrate a title and footnote that repeats across all the pages in my pdf? Your solution works well, but I need a title and footnote to go across all these pages. – Pryore Nov 14 '18 at 09:39
  • @Pryore go to the link provided. There's a better example using sweave (essentially latex). So latex code should work. https://www.overleaf.com/learn/latex/Headers_and_footers – Amar Mar 20 '19 at 01:04
0

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