【问题标题】:Data.frame header on every PDF page (R - grid & gridExtra)每个 PDF 页面上的 Data.frame 标题(R - grid & gridExtra)
【发布时间】:2020-03-03 19:50:04
【问题描述】:

我正在尝试在多页上打印 100 行数据框的 PDF。继baptiste 提出的model 之后,可以使用{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】:

    诀窍是从原始的子集在每个页面上创建一个新的数据框。在调整行高以适应新的标题行以及保留跨页的行数时会出现困难。

    无论如何,这是完成此任务的更新函数:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-08
      • 1970-01-01
      相关资源
      最近更新 更多