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