# Basic usage - split mtcars into pages of 10 rows
pages <- paginate_table(mtcars, rows_per_page = 10)
length(pages) # Number of pages
# With filled last page for consistent positioning
pages_filled <- paginate_table(mtcars, rows_per_page = 10, fill_empty = "-")
nrow(pages_filled[[1]]) # 10 rows
nrow(pages_filled[[length(pages_filled)]]) # Also 10 rows (filled with empty rows)
# With empty string fill
pages_empty <- paginate_table(mtcars, rows_per_page = 10, fill_empty = " ")
# Without filling (default)
pages_no_fill <- paginate_table(mtcars, rows_per_page = 10)
# Split by a grouping column
pages_by_cyl <- paginate_table(mtcars, split_by = "cyl")
length(pages_by_cyl) # 3 pages (one for each cylinder count: 4, 6, 8)
names(pages_by_cyl) # "4", "6", "8" - named with group values
# Split by column with filling to match maximum page size
pages_by_cyl_filled <- paginate_table(mtcars, split_by = "cyl", fill_empty = "|")
sapply(pages_by_cyl_filled, nrow) # All pages have same number of rows
names(pages_by_cyl_filled) # "4", "6", "8"
# Combine split_by and rows_per_page: split by cylinder, then by 5 rows
pages_combined <- paginate_table(mtcars, split_by = "cyl", rows_per_page = 5)
# Groups with more than 5 rows will be split into multiple pages
names(pages_combined) # e.g., "4", "6", "8", "8", "8" (8 cylinder group split into 3 pages)
# With filling for combined approach
pages_combined_filled <- paginate_table(
mtcars,
split_by = "cyl",
rows_per_page = 5,
fill_empty = "-"
)
library(gridify)
library(gt)
# (to use |> version 4.1.0 of R is required, for lower versions we recommend %>% from magrittr)
library(magrittr)
# Regular Example with gt
pages <- paginate_table(mtcars, rows_per_page = 10, fill_empty = " ")
row_height_pixels <- 10
font_size <- 12
font_type <- "serif"
# Create gridify objects for each page
gridify_list <- lapply(seq_along(pages), function(page) {
gt_table <- gt::gt(pages[[page]]) %>%
gt::tab_options(
table.width = gt::pct(80),
data_row.padding = gt::px(row_height_pixels),
table.font.size = font_size,
table.font.names = font_type
)
gridify(
gt_table,
layout = pharma_layout_A4(global_gpar = grid::gpar(fontfamily = font_type))
) %>%
set_cell("title_1", "My Multi-Page Table") %>%
set_cell("footer_right", paste("Page", page, "of", length(pages)))
})
# Export as multi-page PDF
temp_my_multipage_table_gt_simple <- tempfile(fileext = ".pdf")
export_to(gridify_list, temp_my_multipage_table_gt_simple)
# By var Example with gt
pages <- paginate_table(mtcars, split_by = "cyl")
row_height_pixels <- 10
font_size <- 12
font_type <- "serif"
# Create gridify objects for each page
gridify_list <- lapply(seq_along(pages), function(page) {
gt_table <- gt::gt(pages[[page]]) %>%
gt::tab_options(
table.width = gt::pct(80),
data_row.padding = gt::px(row_height_pixels),
table.font.size = font_size,
table.font.names = font_type
)
gridify(
gt_table,
layout = pharma_layout_A4(global_gpar = grid::gpar(fontfamily = font_type))
) %>%
set_cell("title_1", "My Multi-Page Table") %>%
set_cell("by_line", sprintf("cyl is equal to %s", names(pages)[page])) %>%
set_cell("footer_right", paste("Page", page, "of", length(pages)))
})
# Export as multi-page PDF
temp_my_multipage_table_gt_by <- tempfile(fileext = ".pdf")
export_to(gridify_list, temp_my_multipage_table_gt_by)
Run the code above in your browser using DataLab