Print to PDF File Using Grid.Table in R - Too Many Rows to Fit on One Page

How do I fit a very wide grid.table or tableGrob to fit on a pdf page?

I got this done using font sizes. Not the best solution (requires manual intervention) but maybe someone can contribute something more elegant.

termTable = tableGrob(terms, h.even.alpha=1, h.odd.alpha=1,  v.even.alpha=0.5, v.odd.alpha=1, core.just='left', rows=c(),
gpar.coretext =gpar(fontsize=8),
gpar.coltext=gpar(fontsize=10, fontface='bold'),
gpar.rowtext=gpar(fontsize=10, fontface='bold')
)

Saving dataframe to pdf adjust width

I figured out I could add the scale parameter to ggsave. I wrote a simple function to get the optimal scale:

optimal.scale <- function(w,h, wanted.w, wanted.h) max(c(w/wanted.w, h/wanted.h))

I added 0.1 to the scale to add a margin to the plot such that the text is not directly on the edge of the paper. Then I passed the resulting scale to ggsave

  tg = gridExtra::tableGrob(table
h = grid::convertHeight(sum(tg$heights), "mm", TRUE)
w = grid::convertWidth(sum(tg$widths), "mm", TRUE)
scale = optimal.scale(w,h, 279, 210) + 0.1 #A4 = 279 x 210 in landscape
ggplot2::ggsave("test.pdf", tg, width = 279, height = 210, units = 'mm' , scale = scale)

Now my table fits on the A4:

Sample Image

Data.frame header on every PDF page (R - grid & gridExtra)

The trick is to create a new data frame on each page from subsets of the original. The difficulties arise in adjusting the row height to accommodate the new header row, and in preserving numbering of rows across pages.

Anyway, here is the updated function to accomplish that:

library(magrittr)
library(gridExtra)
library(grid)

set_multipage_pdf <- function(df, paper_size = 'a4', margin = 1.30, landscape = FALSE,
page_cex = 1, table_cex = 1)
{
sizes <- list(a4 = c(larger_size = 29.70, smaller_size = 21.00),
a3 = c(larger_size = 42.00, smaller_size = 29.70),
letter = c(larger_size = 27.94, smaller_size = 21.59),
executive = c(larger_size = 18.41, smaller_size = 26.67))

if (landscape) {
paper_height <- sizes[[paper_size]]['smaller_size'] * page_cex
paper_width <- sizes[[paper_size]]['larger_size'] * page_cex
} else {
paper_height <- sizes[[paper_size]]['larger_size'] * page_cex
paper_width <- sizes[[paper_size]]['smaller_size'] * page_cex
}

tg <- df %>% tableGrob(rows = seq_len(nrow(df)),
theme = ttheme_default(base_size = 5 * table_cex))

fullheight <- convertHeight(sum(tg$heights), "cm", valueOnly = TRUE)
page_margin <- unit(margin, "cm")
margin_cm <- convertHeight(page_margin, "cm", valueOnly = TRUE)
freeheight <- paper_height - margin_cm
npages <- ceiling(fullheight / freeheight)
nrows <- nrow(tg)
heights <- convertHeight(tg$heights, "cm", valueOnly = TRUE) * (nrows + npages)/nrows
rows <- cut(cumsum(heights), include.lowest = FALSE,
breaks = c(0, cumsum(rep(freeheight, npages))))
groups <- split(seq_len(nrows), rows)

gl <- lapply(groups, function(id)
{
df[id,] %>%
tableGrob(rows = id, theme = ttheme_default(base_size = 5 * table_cex))
})

for(page in seq_len(npages)){
if(page > 1) grid.newpage()
grid.draw(gl[[page]])
}

}

Each cluster of data frame on separate page of pdf file

grid.newpage() forces each table to appear on a new page,

pdf("multipage.pdf")
lapply(split(mtcars, mtcars$carb), function(d) {
grid::grid.newpage()
gridExtra::grid.table(d)
}
)
dev.off()

How to write huge rows of data using r tableGrob

it works just fine if you have enough time

library(grid)
library(gridExtra)

pdf("y.pdf", height=22, width=0.05)
grid.table(sample(letters, size = 1001, TRUE), theme=ttheme_minimal(2, padding = unit(c(0,0),"mm")))
dev.off()

enter image description here



Related Topics



Leave a reply



Submit