|
| 1 | +#' Split a data frame into pages for multi-page tables |
| 2 | +#' |
| 3 | +#' @description |
| 4 | +#' A lightweight utility function to split a data frame into pages based on the number of rows per page. |
| 5 | +#' This is useful when creating multi-page tables with `gridify`. |
| 6 | +#' |
| 7 | +#' @param data A data frame to split into pages. |
| 8 | +#' @param rows_per_page Integer or NULL. The maximum number of rows per page. |
| 9 | +#' When used with `split_by`, groups larger than `rows_per_page` will be split into multiple pages. |
| 10 | +#' When used alone, splits the entire dataset by row count. |
| 11 | +#' At least one of `rows_per_page` or `split_by` must be provided. |
| 12 | +#' @param split_by Character string or NULL. Name of a column in `data` to split by. |
| 13 | +#' Each unique value in this column starts a new page. Can be combined with `rows_per_page` |
| 14 | +#' to further split large groups. |
| 15 | +#' At least one of `rows_per_page` or `split_by` must be provided. |
| 16 | +#' @param fill_empty Character string or NULL. When provided, fills incomplete pages |
| 17 | +#' with empty rows to match the target row count. Default is `NULL` (no filling). |
| 18 | +#' Providing a value (e.g., `"|"`, `""`, or `"—"`) automatically enables filling and uses that |
| 19 | +#' value for all cells in the empty rows. This helps maintain consistent vertical positioning |
| 20 | +#' across all pages. The target row count is the maximum page size across all pages. |
| 21 | +#' |
| 22 | +#' @return A list of data frames, one for each page. When `split_by` is used, the list |
| 23 | +#' is named with the group values. If a group spans multiple pages (when combined with |
| 24 | +#' `rows_per_page`), multiple list elements will have the same name. |
| 25 | +#' When only `rows_per_page` is used, returns an unnamed list. |
| 26 | +#' |
| 27 | +#' @details |
| 28 | +#' This is a simple utility to help with the common task of paginating large tables. |
| 29 | +#' After splitting the data, you can use a loop to create multiple `gridify` objects |
| 30 | +#' and export them as a multi-page PDF or separate image files. |
| 31 | +#' |
| 32 | +#' The function does not perform the gridify conversion itself - it only prepares the data. |
| 33 | +#' This keeps the package lightweight and flexible. |
| 34 | +#' |
| 35 | +#' @note This function is designed to work with data frames. |
| 36 | +#' It is suited especially for use with gt package. |
| 37 | +#' |
| 38 | +#' @seealso [gridify()], [export_to()] |
| 39 | +#' |
| 40 | +#' @examples |
| 41 | +#' # Basic usage - split mtcars into pages of 10 rows |
| 42 | +#' pages <- paginate_table(mtcars, rows_per_page = 10) |
| 43 | +#' length(pages) # Number of pages |
| 44 | +#' |
| 45 | +#' # With filled last page for consistent positioning |
| 46 | +#' pages_filled <- paginate_table(mtcars, rows_per_page = 10, fill_empty = "-") |
| 47 | +#' nrow(pages_filled[[1]]) # 10 rows |
| 48 | +#' nrow(pages_filled[[length(pages_filled)]]) # Also 10 rows (filled with empty rows) |
| 49 | +#' |
| 50 | +#' # With empty string fill |
| 51 | +#' pages_empty <- paginate_table(mtcars, rows_per_page = 10, fill_empty = " ") |
| 52 | +#' |
| 53 | +#' # Without filling (default) |
| 54 | +#' pages_no_fill <- paginate_table(mtcars, rows_per_page = 10) |
| 55 | +#' |
| 56 | +#' # Split by a grouping column |
| 57 | +#' pages_by_cyl <- paginate_table(mtcars, split_by = "cyl") |
| 58 | +#' length(pages_by_cyl) # 3 pages (one for each cylinder count: 4, 6, 8) |
| 59 | +#' names(pages_by_cyl) # "4", "6", "8" - named with group values |
| 60 | +#' |
| 61 | +#' # Split by column with filling to match maximum page size |
| 62 | +#' pages_by_cyl_filled <- paginate_table(mtcars, split_by = "cyl", fill_empty = "|") |
| 63 | +#' sapply(pages_by_cyl_filled, nrow) # All pages have same number of rows |
| 64 | +#' names(pages_by_cyl_filled) # "4", "6", "8" |
| 65 | +#' |
| 66 | +#' # Combine split_by and rows_per_page: split by cylinder, then by 5 rows |
| 67 | +#' pages_combined <- paginate_table(mtcars, split_by = "cyl", rows_per_page = 5) |
| 68 | +#' # Groups with more than 5 rows will be split into multiple pages |
| 69 | +#' names(pages_combined) # e.g., "4", "6", "8", "8", "8" (8 cylinder group split into 3 pages) |
| 70 | +#' |
| 71 | +#' # With filling for combined approach |
| 72 | +#' pages_combined_filled <- paginate_table( |
| 73 | +#' mtcars, |
| 74 | +#' split_by = "cyl", |
| 75 | +#' rows_per_page = 5, |
| 76 | +#' fill_empty = "-" |
| 77 | +#' ) |
| 78 | +#' |
| 79 | +#' |
| 80 | +#' library(gridify) |
| 81 | +#' library(gt) |
| 82 | +#' # (to use |> version 4.1.0 of R is required, for lower versions we recommend %>% from magrittr) |
| 83 | +#' library(magrittr) |
| 84 | +#' |
| 85 | +#' # Regular Example with gt |
| 86 | +#' |
| 87 | +#' pages <- paginate_table(mtcars, rows_per_page = 10, fill_empty = " ") |
| 88 | +#' |
| 89 | +#' row_height_pixels <- 10 |
| 90 | +#' font_size <- 12 |
| 91 | +#' font_type <- "serif" |
| 92 | +#' |
| 93 | +#' # Create gridify objects for each page |
| 94 | +#' gridify_list <- lapply(seq_along(pages), function(page) { |
| 95 | +#' gt_table <- gt::gt(pages[[page]]) %>% |
| 96 | +#' gt::tab_options( |
| 97 | +#' table.width = gt::pct(80), |
| 98 | +#' data_row.padding = gt::px(row_height_pixels), |
| 99 | +#' table.font.size = font_size, |
| 100 | +#' table.font.names = font_type |
| 101 | +#' ) |
| 102 | +#' |
| 103 | +#' gridify( |
| 104 | +#' gt_table, |
| 105 | +#' layout = pharma_layout_A4(global_gpar = grid::gpar(fontfamily = font_type)) |
| 106 | +#' ) %>% |
| 107 | +#' set_cell("title_1", "My Multi-Page Table") %>% |
| 108 | +#' set_cell("footer_right", paste("Page", page, "of", length(pages))) |
| 109 | +#' }) |
| 110 | +#' |
| 111 | +#' # Export as multi-page PDF |
| 112 | +#' temp_my_multipage_table_gt_simple <- tempfile(fileext = ".pdf") |
| 113 | +#' export_to(gridify_list, temp_my_multipage_table_gt_simple) |
| 114 | +#' |
| 115 | +#' # By var Example with gt |
| 116 | +#' |
| 117 | +#' pages <- paginate_table(mtcars, split_by = "cyl") |
| 118 | +#' |
| 119 | +#' row_height_pixels <- 10 |
| 120 | +#' font_size <- 12 |
| 121 | +#' font_type <- "serif" |
| 122 | +#' |
| 123 | +#' # Create gridify objects for each page |
| 124 | +#' gridify_list <- lapply(seq_along(pages), function(page) { |
| 125 | +#' gt_table <- gt::gt(pages[[page]]) %>% |
| 126 | +#' gt::tab_options( |
| 127 | +#' table.width = gt::pct(80), |
| 128 | +#' data_row.padding = gt::px(row_height_pixels), |
| 129 | +#' table.font.size = font_size, |
| 130 | +#' table.font.names = font_type |
| 131 | +#' ) |
| 132 | +#' |
| 133 | +#' gridify( |
| 134 | +#' gt_table, |
| 135 | +#' layout = pharma_layout_A4(global_gpar = grid::gpar(fontfamily = font_type)) |
| 136 | +#' ) %>% |
| 137 | +#' set_cell("title_1", "My Multi-Page Table") %>% |
| 138 | +#' set_cell("by_line", sprintf("cyl is equal to %s", names(pages)[page])) %>% |
| 139 | +#' set_cell("footer_right", paste("Page", page, "of", length(pages))) |
| 140 | +#' }) |
| 141 | +#' |
| 142 | +#' # Export as multi-page PDF |
| 143 | +#' temp_my_multipage_table_gt_by <- tempfile(fileext = ".pdf") |
| 144 | +#' export_to(gridify_list, temp_my_multipage_table_gt_by) |
| 145 | +#' |
| 146 | +#' @export |
| 147 | +paginate_table <- function( |
| 148 | + data, |
| 149 | + rows_per_page = NULL, |
| 150 | + split_by = NULL, |
| 151 | + fill_empty = NULL) { |
| 152 | + |
| 153 | + if (!is.data.frame(data)) { |
| 154 | + stop("`data` must be a data frame.") |
| 155 | + } |
| 156 | + |
| 157 | + # Check that at least one of rows_per_page or split_by is provided |
| 158 | + if (is.null(rows_per_page) && is.null(split_by)) { |
| 159 | + stop("At least one of `rows_per_page` or `split_by` must be provided.") |
| 160 | + } |
| 161 | + |
| 162 | + # Validate rows_per_page if provided |
| 163 | + if (!is.null(rows_per_page)) { |
| 164 | + if (!(is.numeric(rows_per_page) && length(rows_per_page) == 1 && rows_per_page > 0)) { |
| 165 | + stop("`rows_per_page` must be a positive integer.") |
| 166 | + } |
| 167 | + rows_per_page <- as.integer(rows_per_page) |
| 168 | + } |
| 169 | + |
| 170 | + # Validate split_by if provided |
| 171 | + if (!is.null(split_by)) { |
| 172 | + if (!is.character(split_by) || length(split_by) != 1) { |
| 173 | + stop("`split_by` must be a single character string.") |
| 174 | + } |
| 175 | + if (!split_by %in% colnames(data)) { |
| 176 | + stop("`split_by` column '", split_by, "' not found in data.") |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + if (!is.null(fill_empty) && (!is.character(fill_empty) || length(fill_empty) != 1)) { |
| 181 | + stop("`fill_empty` must be NULL or a single character string.") |
| 182 | + } |
| 183 | + |
| 184 | + # Split data based on method |
| 185 | + if (!is.null(split_by)) { |
| 186 | + # Split by column values only - keep names |
| 187 | + groups <- split(data, data[[split_by]], drop = TRUE) |
| 188 | + # if rows_per_page is null then take max number of rows of the groups |
| 189 | + if(is.null(rows_per_page)) rows_per_page <- max(unlist(lapply(groups, nrow))) |
| 190 | + } else { |
| 191 | + # don't split by column, only have 1 element in list |
| 192 | + groups <- list(data) |
| 193 | + } |
| 194 | + group_names <- names(groups) |
| 195 | + |
| 196 | + pages <- list() |
| 197 | + page_names <- character() |
| 198 | + |
| 199 | + for(i in seq_along(groups)){ |
| 200 | + group <- groups[[i]] |
| 201 | + group_name <- group_names[i] |
| 202 | + group_rows <- nrow(group) |
| 203 | + |
| 204 | + if (group_rows <= rows_per_page) { |
| 205 | + # Group fits in one page |
| 206 | + pages <- c(pages, list(group)) |
| 207 | + page_names <- c(page_names, group_name) |
| 208 | + } else { |
| 209 | + # Split group into multiple pages |
| 210 | + n_pages <- ceiling(group_rows / rows_per_page) |
| 211 | + page_assignments <- rep( |
| 212 | + seq_len(n_pages), |
| 213 | + each = rows_per_page, |
| 214 | + length.out = group_rows |
| 215 | + ) |
| 216 | + group_pages <- split(group, page_assignments) |
| 217 | + pages <- c(pages, group_pages) |
| 218 | + # Repeat group name for each page in this group |
| 219 | + page_names <- c(page_names, rep(group_name, n_pages)) |
| 220 | + |
| 221 | + } |
| 222 | + } |
| 223 | + names(pages) <- if (!is.null(split_by)) page_names else NULL |
| 224 | + |
| 225 | + # Fill pages if requested |
| 226 | + if (!is.null(fill_empty)) { |
| 227 | + |
| 228 | + pages <- lapply(pages, function(page) { |
| 229 | + page_nrows <- nrow(page) |
| 230 | + |
| 231 | + if (page_nrows < rows_per_page) { |
| 232 | + rows_difference <- rows_per_page - page_nrows |
| 233 | + |
| 234 | + # Create empty rows with specified fill value |
| 235 | + empty_df <- data.frame( |
| 236 | + matrix(fill_empty, nrow = rows_difference, ncol = ncol(data)), |
| 237 | + stringsAsFactors = FALSE |
| 238 | + ) |
| 239 | + colnames(empty_df) <- colnames(data) |
| 240 | + |
| 241 | + # Append to page |
| 242 | + page <- rbind(page, empty_df) |
| 243 | + } |
| 244 | + |
| 245 | + page |
| 246 | + }) |
| 247 | + } |
| 248 | + |
| 249 | + pages |
| 250 | +} |
0 commit comments