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