Data.frame header 在每个 PDF 页面上(R - grid & gridExtra)

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

我正在尝试在多个页面上打印 100 行数据框的 PDF。按照 model proposed by baptiste,可以使用 {grid} 和 {gridExtra} 包来完成。

然而,结果是只有第一页显示header,其他页面被省略。我想知道我是否可以以所有页面都显示 header.

的方式打印 PDF

谢谢丹

改编巴蒂斯特的回答:

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)
  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) tg[id,])

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

}

# TEST:
df <- iris[sample(nrow(iris), 187, TRUE),]

pdf('test.pdf', paper = 'a4', width = 0, height = 0)
set_multipage_pdf(df, table_cex = 1.5)
dev.off()

结果: Model by baptiste

诀窍是根据原始数据的子集在每个页面上创建一个新的数据框。困难在于调整行高以适应新的 header 行,以及保留跨页的行编号。

无论如何,这里是完成此操作的更新函数:

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]])
  }

}