每个PDF页面上的Data.frame标头(R-grid和gridExtra)

问题描述 投票:0回答:1

我正在尝试在多页上打印100行数据框的PDF。遵循model提出的baptiste,可以使用{grid}和{gridExtra}包来实现。

但是,结果是只有第一页显示标题,其他页省略了。我想知道我是否可以以所有页面都显示标题的方式打印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

r pdf-generation gridextra r-grid
1个回答
0
投票

诀窍是从原始子集的每一页上创建一个新的数据框。在调整行高以容纳新的标题行以及保留跨页行的编号方面出现了困难。

无论如何,这是完成该功能的更新功能:

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

}
© www.soinside.com 2019 - 2024. All rights reserved.