我正试图在多个页面上打印一个100行数据帧的PDF。根据baptiste提出的模型,可以使用{grid}和{gridExtra}包来实现。
然而,结果是只有第一页显示标题,而在其他页面中被省略。我想知道我是否可以打印PDF,使所有页面都显示标题。
谢谢,Dan
改编baptiste的答案:
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()
结果:baptiste 模型
诀窍是从原始数据的子集在每个页面上创建一个新的数据帧。在调整行高度以适应新的标题行以及在保留页面之间的行编号时会出现困难。
无论如何,这里有更新的功能来实现这一点:
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]])
}
}