Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
686 views
in Technique[技术] by (71.8m points)

print to pdf file using grid.table in r - too many rows to fit on one page

I'm trying to output a dataframe of about 40 rows and 5 columns to a .pdf file using grid.table in gridExtra package of R.

However, 40 rows is too long for a page so the .pdf file only shows part of the dataframe. I want to know if I can print two columns on one page so all of the rows show up on one page. Alternatively, I need to know how to print the dataframe over multiple pages. Thanks, John

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try this for drawing table on a pdf file that span multiple pages using gridExtra package:

  1. Adjust pdf device aspect ratio

    pdf(file = myfile.pdf, height = 12, width = 26)
    
  2. Split the large data frame into chunks and call grid.newpage before drawing a table.

    require(gridExtra)
    pdf(file = myfile.pdf, height = 12, width = 26)
    grid.newpage()
    grid.table(sga_hits[1:38, ], show.rownames = FALSE)
    grid.newpage()
    grid.table(sga_hits[39:75, ], show.rownames = FALSE)
    dev.off()
    

Automate the above as follows:

    require(gridExtra)
    pdf(file = myfile.pdf, height = 12, width = 26)
    total_rows_per_page = 38 
    start_row = 1 

    if(total_rows_per_page > nrow(sga_hits)){
         end_row = nrow(sga_hits)
    }else {
         end_row = total_rows_per_page 
    }    

    for(i in 1:ceiling(nrow(sga_hits)/total_rows_per_page)){

       grid.newpage()   

       grid.table(sga_hits[start_row:end_row, ], show.rownames = FALSE)

       start_row = end_row + 1

       if((total_rows_per_page + end_row) < nrow(sga_hits)){

            end_row = total_rows_per_page + end_row

       }else {

            end_row = nrow(sga_hits)
       }    
    }

    dev.off()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...