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 | 12b0ade | 2018-01-13 16:19:58 -0500 | [diff] [blame] | 11 | #' @param latex_hline Option controlling the behavior of adding hlines to table. |
| 12 | #' Choose from `full`, `major`, `none`. |
Hao Zhu | 8a160b1 | 2017-06-26 13:41:35 -1000 | [diff] [blame] | 13 | #' |
Hao Zhu | 5a7689e | 2017-06-26 15:37:24 -1000 | [diff] [blame] | 14 | #' @examples dt <- data.frame(a = c(1, 1, 2, 2), b = c("a", "a", "a", "b")) |
| 15 | #' x <- knitr::kable(dt, "html") |
| 16 | #' collapse_rows(x) |
| 17 | #' |
Hao Zhu | f4b3529 | 2017-06-25 22:38:37 -1000 | [diff] [blame] | 18 | #' @export |
Hao Zhu | 12b0ade | 2018-01-13 16:19:58 -0500 | [diff] [blame] | 19 | collapse_rows <- function(kable_input, columns = NULL, |
| 20 | latex_hline = c("full", "major", "none")) { |
Hao Zhu | f4b3529 | 2017-06-25 22:38:37 -1000 | [diff] [blame] | 21 | # if (is.null(columns)) { |
| 22 | # stop("Please specify numeric positions of columns you want to collapse.") |
| 23 | # } |
Hao Zhu | 2a87e8e | 2017-06-14 15:49:33 -0400 | [diff] [blame] | 24 | kable_format <- attr(kable_input, "format") |
| 25 | if (!kable_format %in% c("html", "latex")) { |
Hao Zhu | 401ebd8 | 2018-01-14 17:10:20 -0500 | [diff] [blame] | 26 | warning("Please specify format in kable. kableExtra can customize either ", |
| 27 | "HTML or LaTeX outputs. See https://haozhu233.github.io/kableExtra/ ", |
| 28 | "for details.") |
Hao Zhu | 2a87e8e | 2017-06-14 15:49:33 -0400 | [diff] [blame] | 29 | return(kable_input) |
| 30 | } |
| 31 | if (kable_format == "html") { |
| 32 | return(collapse_rows_html(kable_input, columns)) |
| 33 | } |
| 34 | if (kable_format == "latex") { |
Hao Zhu | 12b0ade | 2018-01-13 16:19:58 -0500 | [diff] [blame] | 35 | latex_hline <- match.arg(latex_hline, c("full", "major", "none")) |
| 36 | return(collapse_rows_latex(kable_input, columns, latex_hline)) |
Hao Zhu | 2a87e8e | 2017-06-14 15:49:33 -0400 | [diff] [blame] | 37 | } |
| 38 | } |
| 39 | |
| 40 | collapse_rows_html <- function(kable_input, columns) { |
| 41 | kable_attrs <- attributes(kable_input) |
Hao Zhu | 558c72f | 2017-07-24 15:12:00 -0400 | [diff] [blame] | 42 | kable_xml <- read_kable_as_xml(kable_input) |
Hao Zhu | 2a87e8e | 2017-06-14 15:49:33 -0400 | [diff] [blame] | 43 | kable_tbody <- xml_tpart(kable_xml, "tbody") |
| 44 | |
| 45 | 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] | 46 | if (is.null(columns)) { |
| 47 | columns <- seq(1, ncol(kable_dt)) |
| 48 | } |
Hao Zhu | 2345676 | 2018-03-26 12:30:10 -0400 | [diff] [blame] | 49 | if (!is.null(kable_attrs$header_above)) { |
| 50 | kable_dt_col_names <- unlist(kable_dt[kable_attrs$header_above, ]) |
| 51 | kable_dt <- kable_dt[-(1:kable_attrs$header_above),] |
| 52 | names(kable_dt) <- kable_dt_col_names |
| 53 | } |
| 54 | kable_dt$row_id <- seq(nrow(kable_dt)) |
Hao Zhu | 2a87e8e | 2017-06-14 15:49:33 -0400 | [diff] [blame] | 55 | collapse_matrix <- collapse_row_matrix(kable_dt, columns) |
| 56 | |
| 57 | for (i in 1:nrow(collapse_matrix)) { |
| 58 | matrix_row <- collapse_matrix[i, ] |
Hao Zhu | 38cdcdb | 2017-06-27 09:08:30 -1000 | [diff] [blame] | 59 | names(matrix_row) <- names(collapse_matrix) |
Hao Zhu | 3166f06 | 2017-06-26 07:51:46 -1000 | [diff] [blame] | 60 | target_row <- xml_child(kable_tbody, i) |
| 61 | row_node_rm_count <- 0 |
| 62 | for (j in 1:length(matrix_row)) { |
| 63 | collapsing_col <- as.numeric(sub("x", "", names(matrix_row)[j])) - |
| 64 | row_node_rm_count |
| 65 | target_cell <- xml_child(target_row, collapsing_col) |
| 66 | if (matrix_row[j] == 0) { |
| 67 | xml_remove(target_cell) |
| 68 | row_node_rm_count <- row_node_rm_count + 1 |
| 69 | } else if (matrix_row[j] != 1) { |
| 70 | xml_attr(target_cell, "rowspan") <- matrix_row[j] |
| 71 | xml_attr(target_cell, "style") <- paste0( |
| 72 | xml_attr(target_cell, "style"), |
| 73 | "vertical-align: middle !important;") |
Hao Zhu | 2a87e8e | 2017-06-14 15:49:33 -0400 | [diff] [blame] | 74 | } |
| 75 | } |
| 76 | } |
| 77 | |
Hao Zhu | f2dfd14 | 2017-07-24 14:43:28 -0400 | [diff] [blame] | 78 | out <- as_kable_xml(kable_xml) |
Hao Zhu | 2a87e8e | 2017-06-14 15:49:33 -0400 | [diff] [blame] | 79 | attributes(out) <- kable_attrs |
Hao Zhu | f210083 | 2018-01-11 16:20:29 -0500 | [diff] [blame] | 80 | if (!"kableExtra" %in% class(out)) class(out) <- c("kableExtra", class(out)) |
Hao Zhu | 2a87e8e | 2017-06-14 15:49:33 -0400 | [diff] [blame] | 81 | return(out) |
| 82 | } |
| 83 | |
Hao Zhu | f4b3529 | 2017-06-25 22:38:37 -1000 | [diff] [blame] | 84 | collapse_row_matrix <- function(kable_dt, columns, html = T) { |
| 85 | if (html) { |
| 86 | column_block <- function(x) c(x, rep(0, x - 1)) |
| 87 | } else { |
| 88 | column_block <- function(x) c(rep(0, x - 1), x) |
| 89 | } |
| 90 | mapping_matrix <- list() |
| 91 | for (i in columns) { |
| 92 | mapping_matrix[[paste0("x", i)]] <- unlist(lapply( |
| 93 | rle(kable_dt[, i])$length, column_block)) |
| 94 | } |
| 95 | mapping_matrix <- data.frame(mapping_matrix) |
| 96 | return(mapping_matrix) |
| 97 | } |
| 98 | |
Hao Zhu | 12b0ade | 2018-01-13 16:19:58 -0500 | [diff] [blame] | 99 | collapse_rows_latex <- function(kable_input, columns, latex_hline) { |
Hao Zhu | f4b3529 | 2017-06-25 22:38:37 -1000 | [diff] [blame] | 100 | table_info <- magic_mirror(kable_input) |
Hao Zhu | 064990d | 2017-10-17 18:08:42 -0400 | [diff] [blame] | 101 | out <- enc2utf8(as.character(kable_input)) |
| 102 | |
Hao Zhu | f4b3529 | 2017-06-25 22:38:37 -1000 | [diff] [blame] | 103 | if (is.null(columns)) { |
| 104 | columns <- seq(1, table_info$ncol) |
| 105 | } |
Hao Zhu | 064990d | 2017-10-17 18:08:42 -0400 | [diff] [blame] | 106 | |
Hao Zhu | f4b3529 | 2017-06-25 22:38:37 -1000 | [diff] [blame] | 107 | contents <- table_info$contents |
| 108 | kable_dt <- kable_dt_latex(contents) |
Hao Zhu | 01b15b8 | 2018-01-12 17:48:21 -0500 | [diff] [blame] | 109 | collapse_matrix <- collapse_row_matrix(kable_dt, columns, html = FALSE) |
Hao Zhu | f4b3529 | 2017-06-25 22:38:37 -1000 | [diff] [blame] | 110 | |
| 111 | new_kable_dt <- kable_dt |
Hao Zhu | f4b3529 | 2017-06-25 22:38:37 -1000 | [diff] [blame] | 112 | for (j in seq(1:ncol(collapse_matrix))) { |
| 113 | column_align <- table_info$align_vector_origin[columns[j]] |
| 114 | column_width <- ifelse( |
| 115 | is.null(table_info$column_width[[paste0("column_", columns[j])]]), |
| 116 | "*", table_info$column_width[paste0("column_", columns[j])]) |
| 117 | for (i in seq(1:nrow(collapse_matrix))) { |
| 118 | new_kable_dt[i, j] <- collapse_new_dt_item( |
| 119 | kable_dt[i, j], collapse_matrix[i, j], column_width, align = column_align |
| 120 | ) |
| 121 | } |
| 122 | } |
Hao Zhu | 654c91f | 2017-07-03 14:03:34 -0400 | [diff] [blame] | 123 | |
| 124 | midrule_matrix <- collapse_row_matrix(kable_dt, seq(1, table_info$ncol), |
| 125 | html = F) |
| 126 | midrule_matrix[setdiff(seq(1, table_info$ncol), columns)] <- 1 |
| 127 | |
| 128 | ex_bottom <- length(contents) - 1 |
| 129 | contents[2:ex_bottom] <- paste0(contents[2:ex_bottom], "\\\\\\\\") |
| 130 | if (!table_info$booktabs) { |
| 131 | contents[2:ex_bottom] <- paste0(contents[2:ex_bottom], "\n\\\\hline") |
| 132 | } |
Hao Zhu | 01b15b8 | 2018-01-12 17:48:21 -0500 | [diff] [blame] | 133 | |
| 134 | new_contents <- c() |
Hao Zhu | f4b3529 | 2017-06-25 22:38:37 -1000 | [diff] [blame] | 135 | for (i in seq(1:nrow(collapse_matrix))) { |
| 136 | new_contents[i] <- paste0(new_kable_dt[i, ], collapse = " & ") |
Hao Zhu | 12b0ade | 2018-01-13 16:19:58 -0500 | [diff] [blame] | 137 | table_info$contents[i + 1] <- new_contents[i] |
Hao Zhu | 654c91f | 2017-07-03 14:03:34 -0400 | [diff] [blame] | 138 | if (i != nrow(collapse_matrix)) { |
Hao Zhu | 12b0ade | 2018-01-13 16:19:58 -0500 | [diff] [blame] | 139 | row_midrule <- switch( |
| 140 | latex_hline, |
| 141 | "none" = "", |
| 142 | "full" = midline_groups(which(as.numeric(midrule_matrix[i, ]) > 0), |
| 143 | table_info$booktabs), |
| 144 | "major" = ifelse( |
| 145 | sum(as.numeric(midrule_matrix[i, ]) > 0) == ncol(midrule_matrix), |
| 146 | midline_groups(which(as.numeric(midrule_matrix[i, ]) > 0), |
| 147 | table_info$booktabs), |
| 148 | "" |
| 149 | ) |
| 150 | ) |
Hao Zhu | 654c91f | 2017-07-03 14:03:34 -0400 | [diff] [blame] | 151 | new_contents[i] <- paste0(new_contents[i], "\\\\\\\\\n", row_midrule) |
| 152 | } |
Hao Zhu | f4b3529 | 2017-06-25 22:38:37 -1000 | [diff] [blame] | 153 | out <- sub(contents[i + 1], new_contents[i], out) |
| 154 | } |
Hao Zhu | 8f20299 | 2017-07-15 02:20:18 -0400 | [diff] [blame] | 155 | out <- gsub("\\\\addlinespace\n", "", out) |
Hao Zhu | f4b3529 | 2017-06-25 22:38:37 -1000 | [diff] [blame] | 156 | |
| 157 | out <- structure(out, format = "latex", class = "knitr_kable") |
| 158 | table_info$collapse_rows <- TRUE |
| 159 | attr(out, "kable_meta") <- table_info |
| 160 | return(out) |
| 161 | } |
| 162 | |
| 163 | kable_dt_latex <- function(x) { |
| 164 | data.frame(do.call(rbind, str_split(x[-1], " & ")), stringsAsFactors = FALSE) |
| 165 | } |
| 166 | |
| 167 | collapse_new_dt_item <- function(x, span, width = NULL, align) { |
| 168 | if (span == 0) return("") |
| 169 | if (span == 1) return(x) |
| 170 | out <- paste0( |
| 171 | "\\\\multirow\\{", -span, "\\}\\{", |
| 172 | ifelse(is.null(width), "\\*", width), |
| 173 | "\\}\\{", |
| 174 | switch(align, |
| 175 | "l" = "\\\\raggedright\\\\arraybackslash ", |
| 176 | "c" = "\\\\centering\\\\arraybackslash ", |
| 177 | "r" = "\\\\raggedleft\\\\arraybackslash "), |
| 178 | x, "\\}" |
| 179 | ) |
| 180 | return(out) |
Hao Zhu | 2a87e8e | 2017-06-14 15:49:33 -0400 | [diff] [blame] | 181 | } |
Hao Zhu | 654c91f | 2017-07-03 14:03:34 -0400 | [diff] [blame] | 182 | |
| 183 | midline_groups <- function(x, booktabs = T) { |
| 184 | diffs <- c(1, diff(x)) |
| 185 | start_indexes <- c(1, which(diffs > 1)) |
Hao Zhu | 12b0ade | 2018-01-13 16:19:58 -0500 | [diff] [blame] | 186 | end_indexes <- c(start_indexes - 1, length(x)) |
Hao Zhu | 654c91f | 2017-07-03 14:03:34 -0400 | [diff] [blame] | 187 | ranges <- paste0(x[start_indexes], "-", x[end_indexes]) |
| 188 | if (booktabs) { |
| 189 | out <- paste0("\\\\cmidrule{", ranges, "}") |
| 190 | } else { |
| 191 | out <- paste0("\\\\cline{", ranges, "}") |
| 192 | } |
| 193 | out <- paste0(out, collapse = "\n") |
| 194 | return(out) |
| 195 | } |