Hao Zhu | 3166f06 | 2017-06-26 07:51:46 -1000 | [diff] [blame] | 1 | #' Collapse repeated rows to multirow cell |
Hao Zhu | 2a87e8e | 2017-06-14 15:49:33 -0400 | [diff] [blame] | 2 | #' |
Hao Zhu | 8a160b1 | 2017-06-26 13:41:35 -1000 | [diff] [blame] | 3 | #' @description Collapse same values in columns into multirow cells. This |
| 4 | #' feature does similar things with `group_rows`. However, unlike `group_rows`, |
| 5 | #' it analyzes existing columns, finds out rows that can be grouped together, |
| 6 | #' and make them multirow cells. Note that if you want to use `column_spec` to |
| 7 | #' specify column styles, you should use `column_spec` before `collapse_rows`. |
| 8 | #' |
| 9 | #' @param kable_input Output of `knitr::kable()` with `format` specified |
Hao Zhu | 5e2528e | 2020-08-03 09:16:34 -0400 | [diff] [blame] | 10 | #' @param columns A numeric value or vector indicating in which column(s) rows |
Jakob Richter | aebd829 | 2018-10-31 16:27:29 +0100 | [diff] [blame] | 11 | #' need to be collapsed. |
Hao Zhu | ec16936 | 2018-05-21 01:05:29 -0400 | [diff] [blame] | 12 | #' @param valign Select from "top", "middle"(default), "bottom". The reason why |
| 13 | #' "top" is not default is that the multirow package on CRAN win-builder is |
| 14 | #' not up to date. |
Hao Zhu | 12b0ade | 2018-01-13 16:19:58 -0500 | [diff] [blame] | 15 | #' @param latex_hline Option controlling the behavior of adding hlines to table. |
Hao Zhu | 876bcb0 | 2020-08-12 23:02:51 -0400 | [diff] [blame] | 16 | #' Choose from `major`, `full`, `none`, `custom` and `linespace`. We changed the default from |
Hao Zhu | 71b30f2 | 2020-08-12 22:47:13 -0400 | [diff] [blame] | 17 | #' `full` to `major` in version 1.2. |
georgegui | eaeb0cd | 2018-03-30 17:39:46 -0500 | [diff] [blame] | 18 | #' @param custom_latex_hline Numeric column positions whose collapsed rows will |
| 19 | #' be separated by hlines. |
| 20 | #' @param row_group_label_position Option controlling positions of row group |
| 21 | #' labels. Choose from `identity`, `stack`. |
| 22 | #' @param row_group_label_fonts A list of arguments that can be supplied to |
| 23 | #' group_rows function to format the row group label when |
| 24 | #' `row_group_label_position` is `stack` |
| 25 | #' @param headers_to_remove Numeric column positions where headers should be |
| 26 | #' removed when they are stacked. |
Hao Zhu | c985894 | 2020-08-11 21:41:09 -0400 | [diff] [blame] | 27 | #' @param target If multiple columns are selected to do collapsing and a target |
| 28 | #' column is specified, this target column will be used to collapse other |
| 29 | #' columns based on the groups of this target column. |
| 30 | #' @param col_names T/F. A LaTeX specific option. If you set `col.names` be |
| 31 | #' `NULL` in your `kable` call, you need to set this option false to let |
| 32 | #' everything work properly. |
Hao Zhu | 71b30f2 | 2020-08-12 22:47:13 -0400 | [diff] [blame] | 33 | #' @param longtable_clean_cut T/F with default T. Multirow cell sometimes are |
| 34 | #' displayed incorrectly around pagebreak. This option forces groups to cut |
| 35 | #' before the end of a page. If you have a group that is longer than 1 page, |
| 36 | #' you need to turn off this option. |
Hao Zhu | 8a160b1 | 2017-06-26 13:41:35 -1000 | [diff] [blame] | 37 | #' |
Hao Zhu | 5a7689e | 2017-06-26 15:37:24 -1000 | [diff] [blame] | 38 | #' @examples dt <- data.frame(a = c(1, 1, 2, 2), b = c("a", "a", "a", "b")) |
| 39 | #' x <- knitr::kable(dt, "html") |
| 40 | #' collapse_rows(x) |
| 41 | #' |
Hao Zhu | f4b3529 | 2017-06-25 22:38:37 -1000 | [diff] [blame] | 42 | #' @export |
Hao Zhu | 12b0ade | 2018-01-13 16:19:58 -0500 | [diff] [blame] | 43 | collapse_rows <- function(kable_input, columns = NULL, |
Hao Zhu | ec16936 | 2018-05-21 01:05:29 -0400 | [diff] [blame] | 44 | valign = c("middle", "top", "bottom"), |
Hao Zhu | 71b30f2 | 2020-08-12 22:47:13 -0400 | [diff] [blame] | 45 | latex_hline = c("major", "full", "none", "custom"), |
georgegui | eaeb0cd | 2018-03-30 17:39:46 -0500 | [diff] [blame] | 46 | row_group_label_position = c('identity', 'stack'), |
| 47 | custom_latex_hline = NULL, |
| 48 | row_group_label_fonts = NULL, |
Hao Zhu | 5e2528e | 2020-08-03 09:16:34 -0400 | [diff] [blame] | 49 | headers_to_remove = NULL, |
Hao Zhu | c985894 | 2020-08-11 21:41:09 -0400 | [diff] [blame] | 50 | target = NULL, |
Hao Zhu | 71b30f2 | 2020-08-12 22:47:13 -0400 | [diff] [blame] | 51 | col_names = TRUE, |
| 52 | longtable_clean_cut = TRUE) { |
Hao Zhu | 2a87e8e | 2017-06-14 15:49:33 -0400 | [diff] [blame] | 53 | kable_format <- attr(kable_input, "format") |
| 54 | if (!kable_format %in% c("html", "latex")) { |
Hao Zhu | 401ebd8 | 2018-01-14 17:10:20 -0500 | [diff] [blame] | 55 | warning("Please specify format in kable. kableExtra can customize either ", |
| 56 | "HTML or LaTeX outputs. See https://haozhu233.github.io/kableExtra/ ", |
| 57 | "for details.") |
Hao Zhu | 2a87e8e | 2017-06-14 15:49:33 -0400 | [diff] [blame] | 58 | return(kable_input) |
| 59 | } |
Hao Zhu | ec16936 | 2018-05-21 01:05:29 -0400 | [diff] [blame] | 60 | valign <- match.arg(valign, c("middle", "top", "bottom")) |
Hao Zhu | 5e2528e | 2020-08-03 09:16:34 -0400 | [diff] [blame] | 61 | if (!is.null(target)) { |
| 62 | if (length(target) > 1 && is.integer(target)) { |
| 63 | stop("target can only be a length 1 integer") |
| 64 | } |
| 65 | } |
Hao Zhu | 2a87e8e | 2017-06-14 15:49:33 -0400 | [diff] [blame] | 66 | if (kable_format == "html") { |
Hao Zhu | 5e2528e | 2020-08-03 09:16:34 -0400 | [diff] [blame] | 67 | return(collapse_rows_html(kable_input, columns, valign, target)) |
Hao Zhu | 2a87e8e | 2017-06-14 15:49:33 -0400 | [diff] [blame] | 68 | } |
| 69 | if (kable_format == "latex") { |
Hao Zhu | 876bcb0 | 2020-08-12 23:02:51 -0400 | [diff] [blame] | 70 | latex_hline <- match.arg(latex_hline, c( |
| 71 | "major", "full", "none", "custom", "linespace")) |
georgegui | eaeb0cd | 2018-03-30 17:39:46 -0500 | [diff] [blame] | 72 | row_group_label_position <- match.arg(row_group_label_position, |
| 73 | c('identity', 'stack')) |
Hao Zhu | 5dd3e28 | 2018-05-20 18:39:48 -0400 | [diff] [blame] | 74 | return(collapse_rows_latex(kable_input, columns, latex_hline, valign, |
georgegui | eaeb0cd | 2018-03-30 17:39:46 -0500 | [diff] [blame] | 75 | row_group_label_position, row_group_label_fonts, custom_latex_hline, |
Hao Zhu | 71b30f2 | 2020-08-12 22:47:13 -0400 | [diff] [blame] | 76 | headers_to_remove, target, col_names, longtable_clean_cut)) |
Hao Zhu | 2a87e8e | 2017-06-14 15:49:33 -0400 | [diff] [blame] | 77 | } |
| 78 | } |
| 79 | |
Hao Zhu | 5e2528e | 2020-08-03 09:16:34 -0400 | [diff] [blame] | 80 | collapse_rows_html <- function(kable_input, columns, valign, target) { |
Hao Zhu | 2a87e8e | 2017-06-14 15:49:33 -0400 | [diff] [blame] | 81 | kable_attrs <- attributes(kable_input) |
Hao Zhu | 5e2528e | 2020-08-03 09:16:34 -0400 | [diff] [blame] | 82 | kable_xml <- kable_as_xml(kable_input) |
Hao Zhu | 2a87e8e | 2017-06-14 15:49:33 -0400 | [diff] [blame] | 83 | kable_tbody <- xml_tpart(kable_xml, "tbody") |
| 84 | |
| 85 | kable_dt <- rvest::html_table(xml2::read_html(as.character(kable_input)))[[1]] |
Hao Zhu | f4b3529 | 2017-06-25 22:38:37 -1000 | [diff] [blame] | 86 | if (is.null(columns)) { |
| 87 | columns <- seq(1, ncol(kable_dt)) |
| 88 | } |
Hao Zhu | 5e2528e | 2020-08-03 09:16:34 -0400 | [diff] [blame] | 89 | if (!is.null(target)) { |
| 90 | if (!target %in% columns) { |
| 91 | stop("target has to be within the range of columns") |
| 92 | } |
| 93 | } |
Hao Zhu | 2345676 | 2018-03-26 12:30:10 -0400 | [diff] [blame] | 94 | if (!is.null(kable_attrs$header_above)) { |
| 95 | kable_dt_col_names <- unlist(kable_dt[kable_attrs$header_above, ]) |
| 96 | kable_dt <- kable_dt[-(1:kable_attrs$header_above),] |
| 97 | names(kable_dt) <- kable_dt_col_names |
| 98 | } |
Hao Zhu | 5e2528e | 2020-08-03 09:16:34 -0400 | [diff] [blame] | 99 | collapse_matrix <- collapse_row_matrix(kable_dt, columns, target = target) |
Hao Zhu | 2a87e8e | 2017-06-14 15:49:33 -0400 | [diff] [blame] | 100 | |
| 101 | for (i in 1:nrow(collapse_matrix)) { |
| 102 | matrix_row <- collapse_matrix[i, ] |
Hao Zhu | 38cdcdb | 2017-06-27 09:08:30 -1000 | [diff] [blame] | 103 | names(matrix_row) <- names(collapse_matrix) |
Hao Zhu | 3166f06 | 2017-06-26 07:51:46 -1000 | [diff] [blame] | 104 | target_row <- xml_child(kable_tbody, i) |
| 105 | row_node_rm_count <- 0 |
| 106 | for (j in 1:length(matrix_row)) { |
| 107 | collapsing_col <- as.numeric(sub("x", "", names(matrix_row)[j])) - |
| 108 | row_node_rm_count |
| 109 | target_cell <- xml_child(target_row, collapsing_col) |
| 110 | if (matrix_row[j] == 0) { |
| 111 | xml_remove(target_cell) |
| 112 | row_node_rm_count <- row_node_rm_count + 1 |
| 113 | } else if (matrix_row[j] != 1) { |
| 114 | xml_attr(target_cell, "rowspan") <- matrix_row[j] |
| 115 | xml_attr(target_cell, "style") <- paste0( |
| 116 | xml_attr(target_cell, "style"), |
Hao Zhu | 5dd3e28 | 2018-05-20 18:39:48 -0400 | [diff] [blame] | 117 | "vertical-align: ", valign, " !important;") |
Hao Zhu | 2a87e8e | 2017-06-14 15:49:33 -0400 | [diff] [blame] | 118 | } |
| 119 | } |
| 120 | } |
| 121 | |
Hao Zhu | f2dfd14 | 2017-07-24 14:43:28 -0400 | [diff] [blame] | 122 | out <- as_kable_xml(kable_xml) |
Hao Zhu | fdff6f4 | 2020-08-09 14:38:10 -0400 | [diff] [blame] | 123 | kable_attrs$collapse_matrix <- collapse_matrix |
Hao Zhu | 2a87e8e | 2017-06-14 15:49:33 -0400 | [diff] [blame] | 124 | attributes(out) <- kable_attrs |
Hao Zhu | f210083 | 2018-01-11 16:20:29 -0500 | [diff] [blame] | 125 | if (!"kableExtra" %in% class(out)) class(out) <- c("kableExtra", class(out)) |
Hao Zhu | 2a87e8e | 2017-06-14 15:49:33 -0400 | [diff] [blame] | 126 | return(out) |
| 127 | } |
| 128 | |
Hao Zhu | 5e2528e | 2020-08-03 09:16:34 -0400 | [diff] [blame] | 129 | split_factor <- function(x) { |
| 130 | group_idx <- seq(1, length(x)) |
| 131 | return(factor(unlist(lapply(group_idx, function(i) {rep(i, x[i])})))) |
| 132 | } |
| 133 | |
| 134 | collapse_row_matrix <- function(kable_dt, columns, html = T, target = NULL) { |
Hao Zhu | f4b3529 | 2017-06-25 22:38:37 -1000 | [diff] [blame] | 135 | if (html) { |
| 136 | column_block <- function(x) c(x, rep(0, x - 1)) |
| 137 | } else { |
| 138 | column_block <- function(x) c(rep(0, x - 1), x) |
| 139 | } |
| 140 | mapping_matrix <- list() |
Hao Zhu | 5e2528e | 2020-08-03 09:16:34 -0400 | [diff] [blame] | 141 | if (is.null(target)) { |
| 142 | for (i in columns) { |
| 143 | mapping_matrix[[paste0("x", i)]] <- unlist(lapply( |
| 144 | rle(kable_dt[, i])$lengths, column_block)) |
| 145 | } |
| 146 | } else { |
| 147 | target_group = split_factor(rle(kable_dt[, target])$lengths) |
| 148 | for (i in columns) { |
| 149 | column_split = split(kable_dt[, i], target_group) |
| 150 | mapping_matrix[[paste0("x", i)]] <- unlist(lapply( |
| 151 | column_split, function(sp) { |
| 152 | lapply(rle(sp)$length, column_block) |
| 153 | })) |
| 154 | } |
Hao Zhu | f4b3529 | 2017-06-25 22:38:37 -1000 | [diff] [blame] | 155 | } |
Hao Zhu | 5e2528e | 2020-08-03 09:16:34 -0400 | [diff] [blame] | 156 | |
Hao Zhu | f4b3529 | 2017-06-25 22:38:37 -1000 | [diff] [blame] | 157 | mapping_matrix <- data.frame(mapping_matrix) |
| 158 | return(mapping_matrix) |
| 159 | } |
| 160 | |
Hao Zhu | 5dd3e28 | 2018-05-20 18:39:48 -0400 | [diff] [blame] | 161 | collapse_rows_latex <- function(kable_input, columns, latex_hline, valign, |
georgegui | eaeb0cd | 2018-03-30 17:39:46 -0500 | [diff] [blame] | 162 | row_group_label_position, row_group_label_fonts, |
Hao Zhu | c985894 | 2020-08-11 21:41:09 -0400 | [diff] [blame] | 163 | custom_latex_hline, headers_to_remove, target, |
Hao Zhu | 71b30f2 | 2020-08-12 22:47:13 -0400 | [diff] [blame] | 164 | col_names, longtable_clean_cut) { |
Hao Zhu | f4b3529 | 2017-06-25 22:38:37 -1000 | [diff] [blame] | 165 | table_info <- magic_mirror(kable_input) |
Hao Zhu | 3fc0e88 | 2018-04-03 16:06:41 -0400 | [diff] [blame] | 166 | out <- solve_enc(kable_input) |
Hao Zhu | 876bcb0 | 2020-08-12 23:02:51 -0400 | [diff] [blame] | 167 | out <- gsub("\\\\addlinespace\n", "", out) |
Hao Zhu | 064990d | 2017-10-17 18:08:42 -0400 | [diff] [blame] | 168 | |
Hao Zhu | 5dd3e28 | 2018-05-20 18:39:48 -0400 | [diff] [blame] | 169 | valign <- switch( |
| 170 | valign, |
| 171 | top = "\\[t\\]", |
| 172 | middle = "", |
| 173 | bottom = "\\[b\\]" |
| 174 | ) |
| 175 | |
Hao Zhu | f4b3529 | 2017-06-25 22:38:37 -1000 | [diff] [blame] | 176 | if (is.null(columns)) { |
| 177 | columns <- seq(1, table_info$ncol) |
| 178 | } |
Hao Zhu | 064990d | 2017-10-17 18:08:42 -0400 | [diff] [blame] | 179 | |
Hao Zhu | f4b3529 | 2017-06-25 22:38:37 -1000 | [diff] [blame] | 180 | contents <- table_info$contents |
Hao Zhu | c985894 | 2020-08-11 21:41:09 -0400 | [diff] [blame] | 181 | kable_dt <- kable_dt_latex(contents, col_names) |
georgegui | eaeb0cd | 2018-03-30 17:39:46 -0500 | [diff] [blame] | 182 | |
Hao Zhu | aa42441 | 2020-08-03 09:21:46 -0400 | [diff] [blame] | 183 | collapse_matrix_rev <- collapse_row_matrix(kable_dt, columns, html = TRUE, |
| 184 | target) |
| 185 | collapse_matrix <- collapse_row_matrix(kable_dt, columns, html = FALSE, |
| 186 | target) |
Hao Zhu | f4b3529 | 2017-06-25 22:38:37 -1000 | [diff] [blame] | 187 | |
| 188 | new_kable_dt <- kable_dt |
Jakob Richter | aebd829 | 2018-10-31 16:27:29 +0100 | [diff] [blame] | 189 | for (j in seq_along(columns)) { |
Hao Zhu | f4b3529 | 2017-06-25 22:38:37 -1000 | [diff] [blame] | 190 | column_align <- table_info$align_vector_origin[columns[j]] |
| 191 | column_width <- ifelse( |
| 192 | is.null(table_info$column_width[[paste0("column_", columns[j])]]), |
| 193 | "*", table_info$column_width[paste0("column_", columns[j])]) |
| 194 | for (i in seq(1:nrow(collapse_matrix))) { |
georgegui | eaeb0cd | 2018-03-30 17:39:46 -0500 | [diff] [blame] | 195 | if(row_group_label_position == 'stack'){ |
Jakob Richter | aebd829 | 2018-10-31 16:27:29 +0100 | [diff] [blame] | 196 | if(columns[j] < ncol(collapse_matrix) || collapse_matrix_rev[i, j] == 0){ |
| 197 | new_kable_dt[i, columns[j]] <- '' |
georgegui | eaeb0cd | 2018-03-30 17:39:46 -0500 | [diff] [blame] | 198 | } |
| 199 | } else { |
Jakob Richter | aebd829 | 2018-10-31 16:27:29 +0100 | [diff] [blame] | 200 | new_kable_dt[i, columns[j]] <- collapse_new_dt_item( |
| 201 | kable_dt[i, columns[j]], collapse_matrix[i, j], column_width, |
Hao Zhu | 5dd3e28 | 2018-05-20 18:39:48 -0400 | [diff] [blame] | 202 | align = column_align, valign = valign |
georgegui | eaeb0cd | 2018-03-30 17:39:46 -0500 | [diff] [blame] | 203 | ) |
| 204 | } |
Hao Zhu | f4b3529 | 2017-06-25 22:38:37 -1000 | [diff] [blame] | 205 | } |
| 206 | } |
Hao Zhu | 654c91f | 2017-07-03 14:03:34 -0400 | [diff] [blame] | 207 | |
| 208 | midrule_matrix <- collapse_row_matrix(kable_dt, seq(1, table_info$ncol), |
Hao Zhu | aa42441 | 2020-08-03 09:21:46 -0400 | [diff] [blame] | 209 | html = FALSE, target) |
Hao Zhu | 654c91f | 2017-07-03 14:03:34 -0400 | [diff] [blame] | 210 | midrule_matrix[setdiff(seq(1, table_info$ncol), columns)] <- 1 |
| 211 | |
| 212 | ex_bottom <- length(contents) - 1 |
| 213 | contents[2:ex_bottom] <- paste0(contents[2:ex_bottom], "\\\\\\\\") |
| 214 | if (!table_info$booktabs) { |
| 215 | contents[2:ex_bottom] <- paste0(contents[2:ex_bottom], "\n\\\\hline") |
| 216 | } |
Hao Zhu | 01b15b8 | 2018-01-12 17:48:21 -0500 | [diff] [blame] | 217 | |
| 218 | new_contents <- c() |
georgegui | eaeb0cd | 2018-03-30 17:39:46 -0500 | [diff] [blame] | 219 | if(row_group_label_position == 'stack'){ |
| 220 | if(is.null(headers_to_remove)) headers_to_remove <- head(columns, -1) |
| 221 | table_info$colnames[headers_to_remove] <- '' |
| 222 | new_header <- paste(table_info$colnames, collapse = ' & ') |
| 223 | out <- sub(contents[1], new_header, out) |
| 224 | table_info$contents[1] <- new_header |
| 225 | } |
| 226 | if(latex_hline == 'custom' & is.null(custom_latex_hline)){ |
| 227 | if(row_group_label_position == 'stack'){ |
| 228 | custom_latex_hline = 1:2 |
| 229 | } else { |
| 230 | custom_latex_hline = 1 |
| 231 | } |
| 232 | } |
Hao Zhu | f4b3529 | 2017-06-25 22:38:37 -1000 | [diff] [blame] | 233 | for (i in seq(1:nrow(collapse_matrix))) { |
| 234 | new_contents[i] <- paste0(new_kable_dt[i, ], collapse = " & ") |
Hao Zhu | 12b0ade | 2018-01-13 16:19:58 -0500 | [diff] [blame] | 235 | table_info$contents[i + 1] <- new_contents[i] |
Hao Zhu | 654c91f | 2017-07-03 14:03:34 -0400 | [diff] [blame] | 236 | if (i != nrow(collapse_matrix)) { |
Hao Zhu | 12b0ade | 2018-01-13 16:19:58 -0500 | [diff] [blame] | 237 | row_midrule <- switch( |
| 238 | latex_hline, |
| 239 | "none" = "", |
Hao Zhu | 71b30f2 | 2020-08-12 22:47:13 -0400 | [diff] [blame] | 240 | "full" = ifelse( |
| 241 | sum(as.numeric(midrule_matrix[i, ]) > 0) == ncol(midrule_matrix), |
| 242 | midline_groups(which(as.numeric(midrule_matrix[i, ]) > 0), |
| 243 | table_info$booktabs), |
| 244 | midline_groups(which(as.numeric(midrule_matrix[i, ]) > 0), |
| 245 | FALSE) |
| 246 | ), |
Hao Zhu | 12b0ade | 2018-01-13 16:19:58 -0500 | [diff] [blame] | 247 | "major" = ifelse( |
| 248 | sum(as.numeric(midrule_matrix[i, ]) > 0) == ncol(midrule_matrix), |
| 249 | midline_groups(which(as.numeric(midrule_matrix[i, ]) > 0), |
| 250 | table_info$booktabs), |
| 251 | "" |
georgegui | eaeb0cd | 2018-03-30 17:39:46 -0500 | [diff] [blame] | 252 | ), |
| 253 | "custom" = ifelse( |
| 254 | sum(as.numeric(midrule_matrix[i, custom_latex_hline])) > 0, |
| 255 | midline_groups(which(as.numeric(midrule_matrix[i, ]) > 0), |
| 256 | table_info$booktabs), |
| 257 | "" |
Hao Zhu | 876bcb0 | 2020-08-12 23:02:51 -0400 | [diff] [blame] | 258 | ), |
| 259 | "linespace"= ifelse( |
| 260 | sum(as.numeric(midrule_matrix[i, ]) > 0) == ncol(midrule_matrix), |
| 261 | "\\\\addlinespace\n", |
| 262 | "" |
| 263 | ) |
Hao Zhu | 12b0ade | 2018-01-13 16:19:58 -0500 | [diff] [blame] | 264 | ) |
Hao Zhu | 654c91f | 2017-07-03 14:03:34 -0400 | [diff] [blame] | 265 | new_contents[i] <- paste0(new_contents[i], "\\\\\\\\\n", row_midrule) |
| 266 | } |
Hao Zhu | b3f26ae | 2020-08-04 00:36:52 -0400 | [diff] [blame] | 267 | out <- sub(contents[i + 1], new_contents[i], out, perl=TRUE) |
Hao Zhu | f4b3529 | 2017-06-25 22:38:37 -1000 | [diff] [blame] | 268 | } |
| 269 | |
Hao Zhu | 71b30f2 | 2020-08-12 22:47:13 -0400 | [diff] [blame] | 270 | if (table_info$tabular == "longtable" & longtable_clean_cut) { |
| 271 | if (max(collapse_matrix) > 50) { |
| 272 | warning("It seems that you have a group larger than 50 rows and span ", |
| 273 | "over a page. You probably want to set longtable_clean_cut to ", |
| 274 | "be FALSE.") |
| 275 | } |
| 276 | if (latex_hline == "full") { |
| 277 | warning("kableExtra 1.2 adds a clean_cut feature to provide better page", |
| 278 | " breaking in collapse_rows. It only works when latex_hline = ", |
| 279 | "'major'. It looks like you have longtable_clean_cut = T while ", |
| 280 | "latex_hline = 'full'. Please change either one of them.") |
| 281 | } |
| 282 | out <- gsub("\\\\\\\\($|\n)", "\\\\\\\\\\\\nopagebreak\\1", out) |
| 283 | out <- gsub("(\\\\cmidrule[{][^}]*[}])", "\\1\\\\pagebreak[0]", out) |
| 284 | } |
Hao Zhu | f4b3529 | 2017-06-25 22:38:37 -1000 | [diff] [blame] | 285 | out <- structure(out, format = "latex", class = "knitr_kable") |
| 286 | table_info$collapse_rows <- TRUE |
Hao Zhu | ebdb3c2 | 2020-08-12 08:27:38 -0400 | [diff] [blame] | 287 | table_info$collapse_matrix <- collapse_matrix |
Hao Zhu | f4b3529 | 2017-06-25 22:38:37 -1000 | [diff] [blame] | 288 | attr(out, "kable_meta") <- table_info |
georgegui | eaeb0cd | 2018-03-30 17:39:46 -0500 | [diff] [blame] | 289 | if(row_group_label_position == 'stack'){ |
| 290 | group_row_index_list <- collapse_rows_index(kable_dt, head(columns, -1)) |
| 291 | out <- collapse_rows_latex_stack(out, group_row_index_list, row_group_label_fonts) |
| 292 | } |
Hao Zhu | f4b3529 | 2017-06-25 22:38:37 -1000 | [diff] [blame] | 293 | return(out) |
| 294 | } |
| 295 | |
Hao Zhu | c985894 | 2020-08-11 21:41:09 -0400 | [diff] [blame] | 296 | kable_dt_latex <- function(x, col_names) { |
| 297 | if (col_names) { |
| 298 | x <- x[-1] |
| 299 | } |
| 300 | data.frame(do.call(rbind, str_split(x, " & ")), stringsAsFactors = FALSE) |
Hao Zhu | f4b3529 | 2017-06-25 22:38:37 -1000 | [diff] [blame] | 301 | } |
| 302 | |
Hao Zhu | 5dd3e28 | 2018-05-20 18:39:48 -0400 | [diff] [blame] | 303 | collapse_new_dt_item <- function(x, span, width = NULL, align, valign) { |
Hao Zhu | f4b3529 | 2017-06-25 22:38:37 -1000 | [diff] [blame] | 304 | if (span == 0) return("") |
| 305 | if (span == 1) return(x) |
| 306 | out <- paste0( |
Hao Zhu | 5dd3e28 | 2018-05-20 18:39:48 -0400 | [diff] [blame] | 307 | "\\\\multirow", valign, "\\{", -span, "\\}\\{", |
Hao Zhu | f4b3529 | 2017-06-25 22:38:37 -1000 | [diff] [blame] | 308 | ifelse(is.null(width), "\\*", width), |
| 309 | "\\}\\{", |
| 310 | switch(align, |
| 311 | "l" = "\\\\raggedright\\\\arraybackslash ", |
| 312 | "c" = "\\\\centering\\\\arraybackslash ", |
| 313 | "r" = "\\\\raggedleft\\\\arraybackslash "), |
| 314 | x, "\\}" |
| 315 | ) |
| 316 | return(out) |
Hao Zhu | 2a87e8e | 2017-06-14 15:49:33 -0400 | [diff] [blame] | 317 | } |
Hao Zhu | 654c91f | 2017-07-03 14:03:34 -0400 | [diff] [blame] | 318 | |
| 319 | midline_groups <- function(x, booktabs = T) { |
| 320 | diffs <- c(1, diff(x)) |
| 321 | start_indexes <- c(1, which(diffs > 1)) |
Hao Zhu | 12b0ade | 2018-01-13 16:19:58 -0500 | [diff] [blame] | 322 | end_indexes <- c(start_indexes - 1, length(x)) |
Hao Zhu | 654c91f | 2017-07-03 14:03:34 -0400 | [diff] [blame] | 323 | ranges <- paste0(x[start_indexes], "-", x[end_indexes]) |
| 324 | if (booktabs) { |
| 325 | out <- paste0("\\\\cmidrule{", ranges, "}") |
| 326 | } else { |
| 327 | out <- paste0("\\\\cline{", ranges, "}") |
| 328 | } |
| 329 | out <- paste0(out, collapse = "\n") |
| 330 | return(out) |
| 331 | } |
georgegui | eaeb0cd | 2018-03-30 17:39:46 -0500 | [diff] [blame] | 332 | |
Hao Zhu | 876bcb0 | 2020-08-12 23:02:51 -0400 | [diff] [blame] | 333 | linespace_groups <- function(x) { |
| 334 | diffs <- c(1, diff(x)) |
| 335 | start_indexes <- c(1, which(diffs > 1)) |
| 336 | end_indexes <- c(start_indexes - 1, length(x)) |
| 337 | ranges <- paste0(x[start_indexes], "-", x[end_indexes]) |
| 338 | out <- paste0("\\\\addlinespace") |
| 339 | out <- paste0(out, collapse = "\n") |
| 340 | return(out) |
| 341 | } |
| 342 | |
georgegui | eaeb0cd | 2018-03-30 17:39:46 -0500 | [diff] [blame] | 343 | |
| 344 | collapse_rows_index <- function(kable_dt, columns) { |
| 345 | format_to_row_index <- function(x){ |
| 346 | x = rle(x) |
| 347 | out = x$lengths |
| 348 | names(out) = x$values |
| 349 | out |
| 350 | } |
| 351 | group_rows_index_list <- lapply(columns, function(x) { |
| 352 | format_to_row_index(kable_dt[, x]) |
| 353 | }) |
| 354 | return(group_rows_index_list) |
| 355 | } |
| 356 | |
| 357 | |
| 358 | collapse_rows_latex_stack <- function(kable_input, group_row_index_list, |
| 359 | row_group_label_fonts){ |
| 360 | merge_lists <- function(default_list, updated_list){ |
| 361 | for(x in names(updated_list)){ |
| 362 | default_list[[x]] <- updated_list[[x]] |
| 363 | } |
| 364 | return(default_list) |
| 365 | } |
| 366 | default_font_list <- list( |
| 367 | list(bold = T, italic = F), |
| 368 | list(bold = F, italic = T), |
| 369 | list(bold = F, italic = F) |
| 370 | ) |
| 371 | n_default_fonts = length(default_font_list) |
| 372 | n_supplied_fonts = length(row_group_label_fonts) |
| 373 | group_row_font_list <- list() |
| 374 | out <- kable_input |
| 375 | for(i in 1:length(group_row_index_list)){ |
| 376 | if(i > n_default_fonts){ |
| 377 | group_row_args <- default_font_list[[n_default_fonts]] |
| 378 | } else { |
| 379 | group_row_args <- default_font_list[[i]] |
| 380 | } |
| 381 | if(i <= n_supplied_fonts){ |
| 382 | group_row_args <- merge_lists(group_row_args, row_group_label_fonts[[i]]) |
| 383 | } |
| 384 | group_row_args <- merge_lists( |
| 385 | list(kable_input = out, index = group_row_index_list[[i]]), |
| 386 | group_row_args) |
| 387 | out <- do.call(group_rows, group_row_args) |
| 388 | } |
| 389 | return(out) |
| 390 | } |