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 |
| 10 | #' @param columns Numeric column positions where rows need to be collapsed. |
Hao Zhu | ec16936 | 2018-05-21 01:05:29 -0400 | [diff] [blame^] | 11 | #' @param valign Select from "top", "middle"(default), "bottom". The reason why |
| 12 | #' "top" is not default is that the multirow package on CRAN win-builder is |
| 13 | #' not up to date. |
Hao Zhu | 12b0ade | 2018-01-13 16:19:58 -0500 | [diff] [blame] | 14 | #' @param latex_hline Option controlling the behavior of adding hlines to table. |
georgegui | eaeb0cd | 2018-03-30 17:39:46 -0500 | [diff] [blame] | 15 | #' Choose from `full`, `major`, `none`, `custom`. |
| 16 | #' @param custom_latex_hline Numeric column positions whose collapsed rows will |
| 17 | #' be separated by hlines. |
| 18 | #' @param row_group_label_position Option controlling positions of row group |
| 19 | #' labels. Choose from `identity`, `stack`. |
| 20 | #' @param row_group_label_fonts A list of arguments that can be supplied to |
| 21 | #' group_rows function to format the row group label when |
| 22 | #' `row_group_label_position` is `stack` |
| 23 | #' @param headers_to_remove Numeric column positions where headers should be |
| 24 | #' removed when they are stacked. |
Hao Zhu | 8a160b1 | 2017-06-26 13:41:35 -1000 | [diff] [blame] | 25 | #' |
Hao Zhu | 5a7689e | 2017-06-26 15:37:24 -1000 | [diff] [blame] | 26 | #' @examples dt <- data.frame(a = c(1, 1, 2, 2), b = c("a", "a", "a", "b")) |
| 27 | #' x <- knitr::kable(dt, "html") |
| 28 | #' collapse_rows(x) |
| 29 | #' |
Hao Zhu | f4b3529 | 2017-06-25 22:38:37 -1000 | [diff] [blame] | 30 | #' @export |
Hao Zhu | 12b0ade | 2018-01-13 16:19:58 -0500 | [diff] [blame] | 31 | collapse_rows <- function(kable_input, columns = NULL, |
Hao Zhu | ec16936 | 2018-05-21 01:05:29 -0400 | [diff] [blame^] | 32 | valign = c("middle", "top", "bottom"), |
georgegui | eaeb0cd | 2018-03-30 17:39:46 -0500 | [diff] [blame] | 33 | latex_hline = c("full", "major", "none", "custom"), |
| 34 | row_group_label_position = c('identity', 'stack'), |
| 35 | custom_latex_hline = NULL, |
| 36 | row_group_label_fonts = NULL, |
| 37 | headers_to_remove = NULL) { |
Hao Zhu | 2a87e8e | 2017-06-14 15:49:33 -0400 | [diff] [blame] | 38 | kable_format <- attr(kable_input, "format") |
| 39 | if (!kable_format %in% c("html", "latex")) { |
Hao Zhu | 401ebd8 | 2018-01-14 17:10:20 -0500 | [diff] [blame] | 40 | warning("Please specify format in kable. kableExtra can customize either ", |
| 41 | "HTML or LaTeX outputs. See https://haozhu233.github.io/kableExtra/ ", |
| 42 | "for details.") |
Hao Zhu | 2a87e8e | 2017-06-14 15:49:33 -0400 | [diff] [blame] | 43 | return(kable_input) |
| 44 | } |
Hao Zhu | ec16936 | 2018-05-21 01:05:29 -0400 | [diff] [blame^] | 45 | valign <- match.arg(valign, c("middle", "top", "bottom")) |
Hao Zhu | 2a87e8e | 2017-06-14 15:49:33 -0400 | [diff] [blame] | 46 | if (kable_format == "html") { |
Hao Zhu | 5dd3e28 | 2018-05-20 18:39:48 -0400 | [diff] [blame] | 47 | return(collapse_rows_html(kable_input, columns, valign)) |
Hao Zhu | 2a87e8e | 2017-06-14 15:49:33 -0400 | [diff] [blame] | 48 | } |
| 49 | if (kable_format == "latex") { |
georgegui | eaeb0cd | 2018-03-30 17:39:46 -0500 | [diff] [blame] | 50 | latex_hline <- match.arg(latex_hline, c("full", "major", "none", "custom")) |
| 51 | row_group_label_position <- match.arg(row_group_label_position, |
| 52 | c('identity', 'stack')) |
Hao Zhu | 5dd3e28 | 2018-05-20 18:39:48 -0400 | [diff] [blame] | 53 | return(collapse_rows_latex(kable_input, columns, latex_hline, valign, |
georgegui | eaeb0cd | 2018-03-30 17:39:46 -0500 | [diff] [blame] | 54 | row_group_label_position, row_group_label_fonts, custom_latex_hline, |
| 55 | headers_to_remove)) |
Hao Zhu | 2a87e8e | 2017-06-14 15:49:33 -0400 | [diff] [blame] | 56 | } |
| 57 | } |
| 58 | |
Hao Zhu | 5dd3e28 | 2018-05-20 18:39:48 -0400 | [diff] [blame] | 59 | collapse_rows_html <- function(kable_input, columns, valign) { |
Hao Zhu | 2a87e8e | 2017-06-14 15:49:33 -0400 | [diff] [blame] | 60 | kable_attrs <- attributes(kable_input) |
Hao Zhu | 558c72f | 2017-07-24 15:12:00 -0400 | [diff] [blame] | 61 | kable_xml <- read_kable_as_xml(kable_input) |
Hao Zhu | 2a87e8e | 2017-06-14 15:49:33 -0400 | [diff] [blame] | 62 | kable_tbody <- xml_tpart(kable_xml, "tbody") |
| 63 | |
| 64 | 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] | 65 | if (is.null(columns)) { |
| 66 | columns <- seq(1, ncol(kable_dt)) |
| 67 | } |
Hao Zhu | 2345676 | 2018-03-26 12:30:10 -0400 | [diff] [blame] | 68 | if (!is.null(kable_attrs$header_above)) { |
| 69 | kable_dt_col_names <- unlist(kable_dt[kable_attrs$header_above, ]) |
| 70 | kable_dt <- kable_dt[-(1:kable_attrs$header_above),] |
| 71 | names(kable_dt) <- kable_dt_col_names |
| 72 | } |
| 73 | kable_dt$row_id <- seq(nrow(kable_dt)) |
Hao Zhu | 2a87e8e | 2017-06-14 15:49:33 -0400 | [diff] [blame] | 74 | collapse_matrix <- collapse_row_matrix(kable_dt, columns) |
| 75 | |
| 76 | for (i in 1:nrow(collapse_matrix)) { |
| 77 | matrix_row <- collapse_matrix[i, ] |
Hao Zhu | 38cdcdb | 2017-06-27 09:08:30 -1000 | [diff] [blame] | 78 | names(matrix_row) <- names(collapse_matrix) |
Hao Zhu | 3166f06 | 2017-06-26 07:51:46 -1000 | [diff] [blame] | 79 | target_row <- xml_child(kable_tbody, i) |
| 80 | row_node_rm_count <- 0 |
| 81 | for (j in 1:length(matrix_row)) { |
| 82 | collapsing_col <- as.numeric(sub("x", "", names(matrix_row)[j])) - |
| 83 | row_node_rm_count |
| 84 | target_cell <- xml_child(target_row, collapsing_col) |
| 85 | if (matrix_row[j] == 0) { |
| 86 | xml_remove(target_cell) |
| 87 | row_node_rm_count <- row_node_rm_count + 1 |
| 88 | } else if (matrix_row[j] != 1) { |
| 89 | xml_attr(target_cell, "rowspan") <- matrix_row[j] |
| 90 | xml_attr(target_cell, "style") <- paste0( |
| 91 | xml_attr(target_cell, "style"), |
Hao Zhu | 5dd3e28 | 2018-05-20 18:39:48 -0400 | [diff] [blame] | 92 | "vertical-align: ", valign, " !important;") |
Hao Zhu | 2a87e8e | 2017-06-14 15:49:33 -0400 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | } |
| 96 | |
Hao Zhu | f2dfd14 | 2017-07-24 14:43:28 -0400 | [diff] [blame] | 97 | out <- as_kable_xml(kable_xml) |
Hao Zhu | 2a87e8e | 2017-06-14 15:49:33 -0400 | [diff] [blame] | 98 | attributes(out) <- kable_attrs |
Hao Zhu | f210083 | 2018-01-11 16:20:29 -0500 | [diff] [blame] | 99 | if (!"kableExtra" %in% class(out)) class(out) <- c("kableExtra", class(out)) |
Hao Zhu | 2a87e8e | 2017-06-14 15:49:33 -0400 | [diff] [blame] | 100 | return(out) |
| 101 | } |
| 102 | |
Hao Zhu | f4b3529 | 2017-06-25 22:38:37 -1000 | [diff] [blame] | 103 | collapse_row_matrix <- function(kable_dt, columns, html = T) { |
| 104 | if (html) { |
| 105 | column_block <- function(x) c(x, rep(0, x - 1)) |
| 106 | } else { |
| 107 | column_block <- function(x) c(rep(0, x - 1), x) |
| 108 | } |
| 109 | mapping_matrix <- list() |
| 110 | for (i in columns) { |
| 111 | mapping_matrix[[paste0("x", i)]] <- unlist(lapply( |
| 112 | rle(kable_dt[, i])$length, column_block)) |
| 113 | } |
| 114 | mapping_matrix <- data.frame(mapping_matrix) |
| 115 | return(mapping_matrix) |
| 116 | } |
| 117 | |
Hao Zhu | 5dd3e28 | 2018-05-20 18:39:48 -0400 | [diff] [blame] | 118 | collapse_rows_latex <- function(kable_input, columns, latex_hline, valign, |
georgegui | eaeb0cd | 2018-03-30 17:39:46 -0500 | [diff] [blame] | 119 | row_group_label_position, row_group_label_fonts, |
| 120 | custom_latex_hline, headers_to_remove) { |
Hao Zhu | f4b3529 | 2017-06-25 22:38:37 -1000 | [diff] [blame] | 121 | table_info <- magic_mirror(kable_input) |
Hao Zhu | 3fc0e88 | 2018-04-03 16:06:41 -0400 | [diff] [blame] | 122 | out <- solve_enc(kable_input) |
Hao Zhu | 064990d | 2017-10-17 18:08:42 -0400 | [diff] [blame] | 123 | |
Hao Zhu | 5dd3e28 | 2018-05-20 18:39:48 -0400 | [diff] [blame] | 124 | valign <- switch( |
| 125 | valign, |
| 126 | top = "\\[t\\]", |
| 127 | middle = "", |
| 128 | bottom = "\\[b\\]" |
| 129 | ) |
| 130 | |
Hao Zhu | f4b3529 | 2017-06-25 22:38:37 -1000 | [diff] [blame] | 131 | if (is.null(columns)) { |
| 132 | columns <- seq(1, table_info$ncol) |
| 133 | } |
Hao Zhu | 064990d | 2017-10-17 18:08:42 -0400 | [diff] [blame] | 134 | |
Hao Zhu | f4b3529 | 2017-06-25 22:38:37 -1000 | [diff] [blame] | 135 | contents <- table_info$contents |
| 136 | kable_dt <- kable_dt_latex(contents) |
georgegui | eaeb0cd | 2018-03-30 17:39:46 -0500 | [diff] [blame] | 137 | |
| 138 | collapse_matrix_rev <- collapse_row_matrix(kable_dt, columns, html = TRUE) |
Hao Zhu | 01b15b8 | 2018-01-12 17:48:21 -0500 | [diff] [blame] | 139 | collapse_matrix <- collapse_row_matrix(kable_dt, columns, html = FALSE) |
Hao Zhu | f4b3529 | 2017-06-25 22:38:37 -1000 | [diff] [blame] | 140 | |
| 141 | new_kable_dt <- kable_dt |
Hao Zhu | f4b3529 | 2017-06-25 22:38:37 -1000 | [diff] [blame] | 142 | for (j in seq(1:ncol(collapse_matrix))) { |
| 143 | column_align <- table_info$align_vector_origin[columns[j]] |
| 144 | column_width <- ifelse( |
| 145 | is.null(table_info$column_width[[paste0("column_", columns[j])]]), |
| 146 | "*", table_info$column_width[paste0("column_", columns[j])]) |
| 147 | for (i in seq(1:nrow(collapse_matrix))) { |
georgegui | eaeb0cd | 2018-03-30 17:39:46 -0500 | [diff] [blame] | 148 | if(row_group_label_position == 'stack'){ |
| 149 | if(j < ncol(collapse_matrix)|(collapse_matrix_rev[i, j] == 0)){ |
| 150 | new_kable_dt[i, j] <- '' |
| 151 | } |
| 152 | } else { |
| 153 | new_kable_dt[i, j] <- collapse_new_dt_item( |
Hao Zhu | 5dd3e28 | 2018-05-20 18:39:48 -0400 | [diff] [blame] | 154 | kable_dt[i, j], collapse_matrix[i, j], column_width, |
| 155 | align = column_align, valign = valign |
georgegui | eaeb0cd | 2018-03-30 17:39:46 -0500 | [diff] [blame] | 156 | ) |
| 157 | } |
Hao Zhu | f4b3529 | 2017-06-25 22:38:37 -1000 | [diff] [blame] | 158 | } |
| 159 | } |
Hao Zhu | 654c91f | 2017-07-03 14:03:34 -0400 | [diff] [blame] | 160 | |
| 161 | midrule_matrix <- collapse_row_matrix(kable_dt, seq(1, table_info$ncol), |
| 162 | html = F) |
| 163 | midrule_matrix[setdiff(seq(1, table_info$ncol), columns)] <- 1 |
| 164 | |
| 165 | ex_bottom <- length(contents) - 1 |
| 166 | contents[2:ex_bottom] <- paste0(contents[2:ex_bottom], "\\\\\\\\") |
| 167 | if (!table_info$booktabs) { |
| 168 | contents[2:ex_bottom] <- paste0(contents[2:ex_bottom], "\n\\\\hline") |
| 169 | } |
Hao Zhu | 01b15b8 | 2018-01-12 17:48:21 -0500 | [diff] [blame] | 170 | |
| 171 | new_contents <- c() |
georgegui | eaeb0cd | 2018-03-30 17:39:46 -0500 | [diff] [blame] | 172 | if(row_group_label_position == 'stack'){ |
| 173 | if(is.null(headers_to_remove)) headers_to_remove <- head(columns, -1) |
| 174 | table_info$colnames[headers_to_remove] <- '' |
| 175 | new_header <- paste(table_info$colnames, collapse = ' & ') |
| 176 | out <- sub(contents[1], new_header, out) |
| 177 | table_info$contents[1] <- new_header |
| 178 | } |
| 179 | if(latex_hline == 'custom' & is.null(custom_latex_hline)){ |
| 180 | if(row_group_label_position == 'stack'){ |
| 181 | custom_latex_hline = 1:2 |
| 182 | } else { |
| 183 | custom_latex_hline = 1 |
| 184 | } |
| 185 | } |
Hao Zhu | f4b3529 | 2017-06-25 22:38:37 -1000 | [diff] [blame] | 186 | for (i in seq(1:nrow(collapse_matrix))) { |
| 187 | new_contents[i] <- paste0(new_kable_dt[i, ], collapse = " & ") |
Hao Zhu | 12b0ade | 2018-01-13 16:19:58 -0500 | [diff] [blame] | 188 | table_info$contents[i + 1] <- new_contents[i] |
Hao Zhu | 654c91f | 2017-07-03 14:03:34 -0400 | [diff] [blame] | 189 | if (i != nrow(collapse_matrix)) { |
Hao Zhu | 12b0ade | 2018-01-13 16:19:58 -0500 | [diff] [blame] | 190 | row_midrule <- switch( |
| 191 | latex_hline, |
| 192 | "none" = "", |
| 193 | "full" = midline_groups(which(as.numeric(midrule_matrix[i, ]) > 0), |
| 194 | table_info$booktabs), |
| 195 | "major" = ifelse( |
| 196 | sum(as.numeric(midrule_matrix[i, ]) > 0) == ncol(midrule_matrix), |
| 197 | midline_groups(which(as.numeric(midrule_matrix[i, ]) > 0), |
| 198 | table_info$booktabs), |
| 199 | "" |
georgegui | eaeb0cd | 2018-03-30 17:39:46 -0500 | [diff] [blame] | 200 | ), |
| 201 | "custom" = ifelse( |
| 202 | sum(as.numeric(midrule_matrix[i, custom_latex_hline])) > 0, |
| 203 | midline_groups(which(as.numeric(midrule_matrix[i, ]) > 0), |
| 204 | table_info$booktabs), |
| 205 | "" |
| 206 | ) |
Hao Zhu | 12b0ade | 2018-01-13 16:19:58 -0500 | [diff] [blame] | 207 | ) |
Hao Zhu | 654c91f | 2017-07-03 14:03:34 -0400 | [diff] [blame] | 208 | new_contents[i] <- paste0(new_contents[i], "\\\\\\\\\n", row_midrule) |
| 209 | } |
Hao Zhu | f4b3529 | 2017-06-25 22:38:37 -1000 | [diff] [blame] | 210 | out <- sub(contents[i + 1], new_contents[i], out) |
| 211 | } |
Hao Zhu | 8f20299 | 2017-07-15 02:20:18 -0400 | [diff] [blame] | 212 | out <- gsub("\\\\addlinespace\n", "", out) |
Hao Zhu | f4b3529 | 2017-06-25 22:38:37 -1000 | [diff] [blame] | 213 | |
| 214 | out <- structure(out, format = "latex", class = "knitr_kable") |
| 215 | table_info$collapse_rows <- TRUE |
| 216 | attr(out, "kable_meta") <- table_info |
georgegui | eaeb0cd | 2018-03-30 17:39:46 -0500 | [diff] [blame] | 217 | if(row_group_label_position == 'stack'){ |
| 218 | group_row_index_list <- collapse_rows_index(kable_dt, head(columns, -1)) |
| 219 | out <- collapse_rows_latex_stack(out, group_row_index_list, row_group_label_fonts) |
| 220 | } |
Hao Zhu | f4b3529 | 2017-06-25 22:38:37 -1000 | [diff] [blame] | 221 | return(out) |
| 222 | } |
| 223 | |
| 224 | kable_dt_latex <- function(x) { |
| 225 | data.frame(do.call(rbind, str_split(x[-1], " & ")), stringsAsFactors = FALSE) |
| 226 | } |
| 227 | |
Hao Zhu | 5dd3e28 | 2018-05-20 18:39:48 -0400 | [diff] [blame] | 228 | collapse_new_dt_item <- function(x, span, width = NULL, align, valign) { |
Hao Zhu | f4b3529 | 2017-06-25 22:38:37 -1000 | [diff] [blame] | 229 | if (span == 0) return("") |
| 230 | if (span == 1) return(x) |
| 231 | out <- paste0( |
Hao Zhu | 5dd3e28 | 2018-05-20 18:39:48 -0400 | [diff] [blame] | 232 | "\\\\multirow", valign, "\\{", -span, "\\}\\{", |
Hao Zhu | f4b3529 | 2017-06-25 22:38:37 -1000 | [diff] [blame] | 233 | ifelse(is.null(width), "\\*", width), |
| 234 | "\\}\\{", |
| 235 | switch(align, |
| 236 | "l" = "\\\\raggedright\\\\arraybackslash ", |
| 237 | "c" = "\\\\centering\\\\arraybackslash ", |
| 238 | "r" = "\\\\raggedleft\\\\arraybackslash "), |
| 239 | x, "\\}" |
| 240 | ) |
| 241 | return(out) |
Hao Zhu | 2a87e8e | 2017-06-14 15:49:33 -0400 | [diff] [blame] | 242 | } |
Hao Zhu | 654c91f | 2017-07-03 14:03:34 -0400 | [diff] [blame] | 243 | |
| 244 | midline_groups <- function(x, booktabs = T) { |
| 245 | diffs <- c(1, diff(x)) |
| 246 | start_indexes <- c(1, which(diffs > 1)) |
Hao Zhu | 12b0ade | 2018-01-13 16:19:58 -0500 | [diff] [blame] | 247 | end_indexes <- c(start_indexes - 1, length(x)) |
Hao Zhu | 654c91f | 2017-07-03 14:03:34 -0400 | [diff] [blame] | 248 | ranges <- paste0(x[start_indexes], "-", x[end_indexes]) |
| 249 | if (booktabs) { |
| 250 | out <- paste0("\\\\cmidrule{", ranges, "}") |
| 251 | } else { |
| 252 | out <- paste0("\\\\cline{", ranges, "}") |
| 253 | } |
| 254 | out <- paste0(out, collapse = "\n") |
| 255 | return(out) |
| 256 | } |
georgegui | eaeb0cd | 2018-03-30 17:39:46 -0500 | [diff] [blame] | 257 | |
| 258 | |
| 259 | collapse_rows_index <- function(kable_dt, columns) { |
| 260 | format_to_row_index <- function(x){ |
| 261 | x = rle(x) |
| 262 | out = x$lengths |
| 263 | names(out) = x$values |
| 264 | out |
| 265 | } |
| 266 | group_rows_index_list <- lapply(columns, function(x) { |
| 267 | format_to_row_index(kable_dt[, x]) |
| 268 | }) |
| 269 | return(group_rows_index_list) |
| 270 | } |
| 271 | |
| 272 | |
| 273 | collapse_rows_latex_stack <- function(kable_input, group_row_index_list, |
| 274 | row_group_label_fonts){ |
| 275 | merge_lists <- function(default_list, updated_list){ |
| 276 | for(x in names(updated_list)){ |
| 277 | default_list[[x]] <- updated_list[[x]] |
| 278 | } |
| 279 | return(default_list) |
| 280 | } |
| 281 | default_font_list <- list( |
| 282 | list(bold = T, italic = F), |
| 283 | list(bold = F, italic = T), |
| 284 | list(bold = F, italic = F) |
| 285 | ) |
| 286 | n_default_fonts = length(default_font_list) |
| 287 | n_supplied_fonts = length(row_group_label_fonts) |
| 288 | group_row_font_list <- list() |
| 289 | out <- kable_input |
| 290 | for(i in 1:length(group_row_index_list)){ |
| 291 | if(i > n_default_fonts){ |
| 292 | group_row_args <- default_font_list[[n_default_fonts]] |
| 293 | } else { |
| 294 | group_row_args <- default_font_list[[i]] |
| 295 | } |
| 296 | if(i <= n_supplied_fonts){ |
| 297 | group_row_args <- merge_lists(group_row_args, row_group_label_fonts[[i]]) |
| 298 | } |
| 299 | group_row_args <- merge_lists( |
| 300 | list(kable_input = out, index = group_row_index_list[[i]]), |
| 301 | group_row_args) |
| 302 | out <- do.call(group_rows, group_row_args) |
| 303 | } |
| 304 | return(out) |
| 305 | } |