Hao Zhu | 62cdde5 | 2017-05-20 22:16:03 -0400 | [diff] [blame] | 1 | #' Put a few rows of a table into one category |
| 2 | #' |
Hao Zhu | bd95bb2 | 2017-05-22 16:08:49 -0400 | [diff] [blame] | 3 | #' @description Group a few rows in a table together under a label. |
| 4 | #' |
| 5 | #' @param kable_input Output of `knitr::kable()` with `format` specified |
| 6 | #' @param group_label A character string for the name of the group |
| 7 | #' @param start_row A numeric value that tells the function in which row the |
| 8 | #' group starts. Note that the counting excludes header rows and other group |
| 9 | #' labeling rows |
| 10 | #' @param end_row A numeric value that tells the function in which row the group |
| 11 | #' ends. |
Hao Zhu | 49483bf | 2017-09-12 11:21:00 -0400 | [diff] [blame] | 12 | #' @param index A named vector providing the index for robust row-grouping tasks. |
| 13 | #' Basically, you can use it in the same way as `add_header_above()`. |
Hao Zhu | bd95bb2 | 2017-05-22 16:08:49 -0400 | [diff] [blame] | 14 | #' @param label_row_css A character string for any customized css used for the |
| 15 | #' labeling row. By default, the labeling row will have a solid black line |
| 16 | #' underneath. Only useful for HTML documents. |
| 17 | #' @param latex_gap_space A character value telling LaTeX how large the gap |
| 18 | #' between the previous row and the group labeling row. Only useful for LaTeX |
| 19 | #' documents. |
Hao Zhu | ac7e70f | 2017-08-02 00:18:36 -0400 | [diff] [blame] | 20 | #' @param escape A T/F value showing whether special characters should be |
| 21 | #' escaped. |
Salzer | 8f49eb6 | 2018-02-12 22:19:06 -0500 | [diff] [blame] | 22 | #' @param latex_align Adjust justification of group_label in latex only. Value should be "c" for |
Salzer | 1555085 | 2018-02-12 22:09:14 -0500 | [diff] [blame] | 23 | #' centered on row, "r" for right justification, or "l" for left justification. Default |
| 24 | #' Value is "l" If using html, the alignment can be set by using the label_row_css |
| 25 | #' parameter. |
Hao Zhu | 779569e | 2019-04-26 17:06:33 -0400 | [diff] [blame] | 26 | #' @param latex_wrap_text T/F for wrapping long text. Default is off. Whenever |
| 27 | #' it is turned on, the table will take up the entire line. It's recommended |
| 28 | #' to use this with full_width in kable_styling. |
bsalzer | 8593327 | 2018-02-12 17:26:46 -0500 | [diff] [blame] | 29 | #' @param colnum A numeric that determines how many columns the text should span. |
| 30 | #' The default setting will have the text span the entire length. |
georgegui | 4cc925b | 2018-03-01 12:02:45 -0600 | [diff] [blame] | 31 | #' @param bold A T/F value to control whether the text should be bolded. |
| 32 | #' @param italic A T/F value to control whether the text should to be emphasized. |
Brian Salzer | a3f43ad | 2018-03-04 15:03:20 -0500 | [diff] [blame] | 33 | #' @param hline_before A T/F value that addes a horizontal line before the group_row label. Default |
| 34 | #' value is False. |
georgegui | 4cc925b | 2018-03-01 12:02:45 -0600 | [diff] [blame] | 35 | #' @param hline_after A replicate of `hline.after` in xtable. It |
Hao Zhu | 6191e74 | 2018-03-01 13:09:08 -0500 | [diff] [blame] | 36 | #' addes a hline after the row |
georgegui | 4cc925b | 2018-03-01 12:02:45 -0600 | [diff] [blame] | 37 | #' @param extra_latex_after Extra LaTeX text to be added after the row. |
Hao Zhu | 2742ffc | 2018-10-17 11:23:44 -0400 | [diff] [blame] | 38 | #' @param indent A T?F value to control whether list items are indented. |
Hao Zhu | bd95bb2 | 2017-05-22 16:08:49 -0400 | [diff] [blame] | 39 | #' |
Hao Zhu | 78e6122 | 2017-05-24 20:53:35 -0400 | [diff] [blame] | 40 | #' @examples x <- knitr::kable(head(mtcars), "html") |
| 41 | #' # Put Row 2 to Row 5 into a Group and label it as "Group A" |
Hao Zhu | 72917f9 | 2019-03-15 18:41:42 -0400 | [diff] [blame] | 42 | #' pack_rows(x, "Group A", 2, 5) |
Hao Zhu | 78e6122 | 2017-05-24 20:53:35 -0400 | [diff] [blame] | 43 | #' |
Hao Zhu | 62cdde5 | 2017-05-20 22:16:03 -0400 | [diff] [blame] | 44 | #' @export |
Hao Zhu | 49483bf | 2017-09-12 11:21:00 -0400 | [diff] [blame] | 45 | group_rows <- function(kable_input, group_label = NULL, |
| 46 | start_row = NULL, end_row = NULL, |
| 47 | index = NULL, |
Hao Zhu | d972e7f | 2017-05-22 13:27:15 -0400 | [diff] [blame] | 48 | label_row_css = "border-bottom: 1px solid;", |
Hao Zhu | 49483bf | 2017-09-12 11:21:00 -0400 | [diff] [blame] | 49 | latex_gap_space = "0.3em", |
Hao Zhu | 779569e | 2019-04-26 17:06:33 -0400 | [diff] [blame] | 50 | escape = TRUE, latex_align = "l", |
| 51 | latex_wrap_text = FALSE, |
| 52 | colnum = NULL, |
Hao Zhu | 2742ffc | 2018-10-17 11:23:44 -0400 | [diff] [blame] | 53 | bold = TRUE, |
| 54 | italic = FALSE, |
| 55 | hline_before = FALSE, |
| 56 | hline_after = FALSE, |
| 57 | extra_latex_after = NULL, |
| 58 | indent = TRUE) { |
Hao Zhu | 49483bf | 2017-09-12 11:21:00 -0400 | [diff] [blame] | 59 | |
Hao Zhu | 62cdde5 | 2017-05-20 22:16:03 -0400 | [diff] [blame] | 60 | kable_format <- attr(kable_input, "format") |
| 61 | if (!kable_format %in% c("html", "latex")) { |
Hao Zhu | 401ebd8 | 2018-01-14 17:10:20 -0500 | [diff] [blame] | 62 | warning("Please specify format in kable. kableExtra can customize either ", |
| 63 | "HTML or LaTeX outputs. See https://haozhu233.github.io/kableExtra/ ", |
| 64 | "for details.") |
Hao Zhu | 62cdde5 | 2017-05-20 22:16:03 -0400 | [diff] [blame] | 65 | return(kable_input) |
| 66 | } |
Hao Zhu | 49483bf | 2017-09-12 11:21:00 -0400 | [diff] [blame] | 67 | if (is.null(index)) { |
| 68 | if (kable_format == "html") { |
Hao Zhu | 779569e | 2019-04-26 17:06:33 -0400 | [diff] [blame] | 69 | if (!missing(latex_align)) warning("latex_align parameter is not used in HTML Mode, |
Salzer | 1555085 | 2018-02-12 22:09:14 -0500 | [diff] [blame] | 70 | use label_row_css instead.") |
Hao Zhu | 49483bf | 2017-09-12 11:21:00 -0400 | [diff] [blame] | 71 | return(group_rows_html(kable_input, group_label, start_row, end_row, |
Hao Zhu | 2742ffc | 2018-10-17 11:23:44 -0400 | [diff] [blame] | 72 | label_row_css, escape, colnum, indent)) |
Hao Zhu | 49483bf | 2017-09-12 11:21:00 -0400 | [diff] [blame] | 73 | } |
| 74 | if (kable_format == "latex") { |
| 75 | return(group_rows_latex(kable_input, group_label, start_row, end_row, |
georgegui | 4cc925b | 2018-03-01 12:02:45 -0600 | [diff] [blame] | 76 | latex_gap_space, escape, latex_align, colnum, |
Hao Zhu | 2742ffc | 2018-10-17 11:23:44 -0400 | [diff] [blame] | 77 | bold, italic, hline_before, hline_after, |
Hao Zhu | 779569e | 2019-04-26 17:06:33 -0400 | [diff] [blame] | 78 | extra_latex_after, indent, latex_wrap_text)) |
Hao Zhu | 49483bf | 2017-09-12 11:21:00 -0400 | [diff] [blame] | 79 | } |
| 80 | } else { |
| 81 | index <- group_row_index_translator(index) |
| 82 | out <- kable_input |
| 83 | if (kable_format == "html") { |
| 84 | for (i in 1:nrow(index)) { |
Hao Zhu | 779569e | 2019-04-26 17:06:33 -0400 | [diff] [blame] | 85 | if (!missing(latex_align)) warning("latex_align parameter is not used in HTML Mode, |
Salzer | 1555085 | 2018-02-12 22:09:14 -0500 | [diff] [blame] | 86 | use label_row_css instead.") |
Hao Zhu | 49483bf | 2017-09-12 11:21:00 -0400 | [diff] [blame] | 87 | out <- group_rows_html(out, index$header[i], |
| 88 | index$start[i], index$end[i], |
Hao Zhu | 2742ffc | 2018-10-17 11:23:44 -0400 | [diff] [blame] | 89 | label_row_css, escape, colnum, indent) |
Hao Zhu | 49483bf | 2017-09-12 11:21:00 -0400 | [diff] [blame] | 90 | } |
| 91 | } |
| 92 | if (kable_format == "latex") { |
| 93 | for (i in 1:nrow(index)) { |
| 94 | out <- group_rows_latex(out, index$header[i], |
| 95 | index$start[i], index$end[i], |
georgegui | 4cc925b | 2018-03-01 12:02:45 -0600 | [diff] [blame] | 96 | latex_gap_space, escape, latex_align, colnum, |
Hao Zhu | 2742ffc | 2018-10-17 11:23:44 -0400 | [diff] [blame] | 97 | bold, italic, hline_before, hline_after, |
Hao Zhu | 779569e | 2019-04-26 17:06:33 -0400 | [diff] [blame] | 98 | extra_latex_after, indent, latex_wrap_text) |
Hao Zhu | 49483bf | 2017-09-12 11:21:00 -0400 | [diff] [blame] | 99 | } |
| 100 | } |
| 101 | return(out) |
Hao Zhu | 62cdde5 | 2017-05-20 22:16:03 -0400 | [diff] [blame] | 102 | } |
Hao Zhu | 49483bf | 2017-09-12 11:21:00 -0400 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | group_row_index_translator <- function(index) { |
| 106 | index <- standardize_header_input(index) |
| 107 | index$start <- cumsum(c(1, index$colspan))[1:length(index$colspan)] |
| 108 | index$end <- cumsum(index$colspan) |
| 109 | index$header <- trimws(index$header) |
| 110 | index <- index[index$header != "", ] |
| 111 | return(index) |
Hao Zhu | 62cdde5 | 2017-05-20 22:16:03 -0400 | [diff] [blame] | 112 | } |
| 113 | |
Hao Zhu | d972e7f | 2017-05-22 13:27:15 -0400 | [diff] [blame] | 114 | group_rows_html <- function(kable_input, group_label, start_row, end_row, |
Hao Zhu | 2742ffc | 2018-10-17 11:23:44 -0400 | [diff] [blame] | 115 | label_row_css, escape, colnum, indent) { |
Hao Zhu | 62cdde5 | 2017-05-20 22:16:03 -0400 | [diff] [blame] | 116 | kable_attrs <- attributes(kable_input) |
Hao Zhu | 558c72f | 2017-07-24 15:12:00 -0400 | [diff] [blame] | 117 | kable_xml <- read_kable_as_xml(kable_input) |
Hao Zhu | 62cdde5 | 2017-05-20 22:16:03 -0400 | [diff] [blame] | 118 | kable_tbody <- xml_tpart(kable_xml, "tbody") |
| 119 | |
Hao Zhu | ac7e70f | 2017-08-02 00:18:36 -0400 | [diff] [blame] | 120 | if (escape) { |
| 121 | group_label <- escape_html(group_label) |
| 122 | } |
| 123 | |
Hao Zhu | 62cdde5 | 2017-05-20 22:16:03 -0400 | [diff] [blame] | 124 | group_header_rows <- attr(kable_input, "group_header_rows") |
| 125 | group_seq <- seq(start_row, end_row) |
| 126 | if (!is.null(group_header_rows)) { |
| 127 | group_seq <- positions_corrector(group_seq, group_header_rows, |
| 128 | length(xml_children(kable_tbody))) |
| 129 | } |
| 130 | |
| 131 | # Insert a group header row |
| 132 | starting_node <- xml_child(kable_tbody, group_seq[1]) |
bsalzer | 8593327 | 2018-02-12 17:26:46 -0500 | [diff] [blame] | 133 | kable_ncol <- ifelse(is.null(colnum), |
| 134 | length(xml_children(starting_node)), |
| 135 | colnum) |
Hao Zhu | 62cdde5 | 2017-05-20 22:16:03 -0400 | [diff] [blame] | 136 | group_header_row_text <- paste0( |
Hao Zhu | d972e7f | 2017-05-22 13:27:15 -0400 | [diff] [blame] | 137 | '<tr groupLength="', length(group_seq), '"><td colspan="', kable_ncol, |
| 138 | '" style="', label_row_css, '"><strong>', group_label, |
| 139 | "</strong></td></tr>" |
Hao Zhu | 62cdde5 | 2017-05-20 22:16:03 -0400 | [diff] [blame] | 140 | ) |
| 141 | group_header_row <- read_xml(group_header_row_text, options = "COMPACT") |
| 142 | xml_add_sibling(starting_node, group_header_row, .where = "before") |
| 143 | |
| 144 | # add indentations to items |
Hao Zhu | f2dfd14 | 2017-07-24 14:43:28 -0400 | [diff] [blame] | 145 | out <- as_kable_xml(kable_xml) |
Hao Zhu | 62cdde5 | 2017-05-20 22:16:03 -0400 | [diff] [blame] | 146 | attributes(out) <- kable_attrs |
| 147 | attr(out, "group_header_rows") <- c(attr(out, "group_header_rows"), group_seq[1]) |
Hao Zhu | 2742ffc | 2018-10-17 11:23:44 -0400 | [diff] [blame] | 148 | if (indent) { |
| 149 | out <- add_indent_html(out, positions = seq(start_row, end_row)) |
| 150 | } |
Hao Zhu | 62cdde5 | 2017-05-20 22:16:03 -0400 | [diff] [blame] | 151 | return(out) |
| 152 | } |
Hao Zhu | d972e7f | 2017-05-22 13:27:15 -0400 | [diff] [blame] | 153 | |
Hao Zhu | fc14c9b | 2017-05-22 14:03:22 -0400 | [diff] [blame] | 154 | group_rows_latex <- function(kable_input, group_label, start_row, end_row, |
georgegui | 4cc925b | 2018-03-01 12:02:45 -0600 | [diff] [blame] | 155 | gap_space, escape, latex_align, colnum, |
Hao Zhu | 779569e | 2019-04-26 17:06:33 -0400 | [diff] [blame] | 156 | bold = T, italic = F, hline_before = F, hline_after = F, |
| 157 | extra_latex_after = NULL, indent, latex_wrap_text = F) { |
Hao Zhu | d972e7f | 2017-05-22 13:27:15 -0400 | [diff] [blame] | 158 | table_info <- magic_mirror(kable_input) |
Hao Zhu | 3fc0e88 | 2018-04-03 16:06:41 -0400 | [diff] [blame] | 159 | out <- solve_enc(kable_input) |
Hao Zhu | d972e7f | 2017-05-22 13:27:15 -0400 | [diff] [blame] | 160 | |
Hao Zhu | 064990d | 2017-10-17 18:08:42 -0400 | [diff] [blame] | 161 | if (table_info$duplicated_rows) { |
| 162 | dup_fx_out <- fix_duplicated_rows_latex(out, table_info) |
| 163 | out <- dup_fx_out[[1]] |
| 164 | table_info <- dup_fx_out[[2]] |
| 165 | } |
| 166 | |
Hao Zhu | ac7e70f | 2017-08-02 00:18:36 -0400 | [diff] [blame] | 167 | if (escape) { |
Hao Zhu | f94a26f | 2018-04-05 17:42:55 -0400 | [diff] [blame] | 168 | group_label <- input_escape(group_label, latex_align) |
Hao Zhu | ac7e70f | 2017-08-02 00:18:36 -0400 | [diff] [blame] | 169 | } |
| 170 | |
Hao Zhu | f94a26f | 2018-04-05 17:42:55 -0400 | [diff] [blame] | 171 | if (bold) { |
georgegui | 4cc925b | 2018-03-01 12:02:45 -0600 | [diff] [blame] | 172 | group_label <- paste0("\\\\textbf{", group_label, "}") |
| 173 | } |
Hao Zhu | 779569e | 2019-04-26 17:06:33 -0400 | [diff] [blame] | 174 | |
Hao Zhu | f94a26f | 2018-04-05 17:42:55 -0400 | [diff] [blame] | 175 | if (italic) group_label <- paste0("\\\\textit{", group_label, "}") |
Hao Zhu | d972e7f | 2017-05-22 13:27:15 -0400 | [diff] [blame] | 176 | # Add group label |
Hao Zhu | 779569e | 2019-04-26 17:06:33 -0400 | [diff] [blame] | 177 | if (latex_wrap_text) { |
| 178 | latex_align <- switch( |
| 179 | latex_align, |
| 180 | "l" = "p{\\\\linewidth}", |
| 181 | "c" = ">{\\\\centering\\\\arraybackslash}p{\\\\linewidth}", |
| 182 | "r" = ">{\\\\centering\\\\arraybackslash}p{\\\\linewidth}" |
| 183 | ) |
| 184 | } |
| 185 | |
| 186 | |
Hao Zhu | d972e7f | 2017-05-22 13:27:15 -0400 | [diff] [blame] | 187 | if (table_info$booktabs) { |
Hao Zhu | 37dbe3f | 2018-05-14 11:16:06 -0400 | [diff] [blame] | 188 | rowtext <- table_info$contents[start_row + table_info$position_offset] |
georgegui | 4cc925b | 2018-03-01 12:02:45 -0600 | [diff] [blame] | 189 | pre_rowtext <- paste0( |
Hao Zhu | d972e7f | 2017-05-22 13:27:15 -0400 | [diff] [blame] | 190 | "\\\\addlinespace[", gap_space, "]\n", |
Brian Salzer | a3f43ad | 2018-03-04 15:03:20 -0500 | [diff] [blame] | 191 | ifelse(hline_before,"\\\\hline\n", ""), |
bsalzer | 8593327 | 2018-02-12 17:26:46 -0500 | [diff] [blame] | 192 | "\\\\multicolumn{", ifelse(is.null(colnum), |
| 193 | table_info$ncol, |
| 194 | colnum), |
georgegui | 4cc925b | 2018-03-01 12:02:45 -0600 | [diff] [blame] | 195 | "}{", latex_align, "}{", group_label, |
| 196 | "}\\\\\\\\\n", ifelse(hline_after, "\\\\hline\n", '') |
Hao Zhu | d972e7f | 2017-05-22 13:27:15 -0400 | [diff] [blame] | 197 | ) |
Hao Zhu | d972e7f | 2017-05-22 13:27:15 -0400 | [diff] [blame] | 198 | } else { |
Leo | 83f0513 | 2018-05-10 14:12:16 +0800 | [diff] [blame] | 199 | rowtext <- table_info$contents[start_row + 1] |
Hao Zhu | d972e7f | 2017-05-22 13:27:15 -0400 | [diff] [blame] | 200 | rowtext <- paste0("\\\\hline\n", rowtext) |
georgegui | 4cc925b | 2018-03-01 12:02:45 -0600 | [diff] [blame] | 201 | pre_rowtext <- paste0( |
| 202 | "\\\\hline\n\\\\multicolumn{", table_info$ncol, "}{", latex_align,"}{", |
| 203 | group_label, "}\\\\\\\\\n" |
Hao Zhu | d972e7f | 2017-05-22 13:27:15 -0400 | [diff] [blame] | 204 | ) |
| 205 | } |
georgegui | 4cc925b | 2018-03-01 12:02:45 -0600 | [diff] [blame] | 206 | if(!is.null(extra_latex_after)){ |
| 207 | pre_rowtext <- paste0(pre_rowtext, |
| 208 | regex_escape(extra_latex_after, double_backslash = TRUE)) |
| 209 | } |
| 210 | new_rowtext <- paste0(pre_rowtext, rowtext) |
Hao Zhu | 9c3007e | 2020-08-03 13:52:38 -0400 | [diff] [blame] | 211 | if (start_row + 1 == table_info$nrow & |
| 212 | !is.null(table_info$repeat_header_latex)) { |
| 213 | out <- sub(paste0(rowtext, "\\\\\\\\\\*\n"), |
| 214 | paste0(new_rowtext, "\\\\\\\\\\*\n"), |
| 215 | out) |
| 216 | } else { |
| 217 | out <- sub(paste0(rowtext, "\\\\\\\\\n"), |
| 218 | paste0(new_rowtext, "\\\\\\\\\n"), |
| 219 | out) |
| 220 | } |
| 221 | |
Hao Zhu | 8f20299 | 2017-07-15 02:20:18 -0400 | [diff] [blame] | 222 | out <- gsub("\\\\addlinespace\n", "", out) |
Hao Zhu | d2c0f73 | 2017-08-26 10:40:14 -0400 | [diff] [blame] | 223 | out <- structure(out, format = "latex", class = "knitr_kable") |
Hao Zhu | 32f43f7 | 2017-06-20 18:24:54 -0400 | [diff] [blame] | 224 | table_info$group_rows_used <- TRUE |
| 225 | attr(out, "kable_meta") <- table_info |
Hao Zhu | 2742ffc | 2018-10-17 11:23:44 -0400 | [diff] [blame] | 226 | if (indent) { |
| 227 | out <- add_indent_latex(out, seq(start_row, end_row)) |
| 228 | } |
Hao Zhu | d972e7f | 2017-05-22 13:27:15 -0400 | [diff] [blame] | 229 | return(out) |
| 230 | } |
Hao Zhu | be853f7 | 2018-05-20 18:52:26 -0400 | [diff] [blame] | 231 | |
| 232 | #' Automatically figuring out the group_row index |
| 233 | #' |
| 234 | #' @description This helper function allows users to build the `group_row` |
| 235 | #' index more quickly and use `group_rows` in a way that is similar with |
| 236 | #' `collapse_rows`. |
| 237 | #' |
| 238 | #' @param x The index column. A vector. For example `c("a", "a", "b", "b", "b")`` |
| 239 | #' |
| 240 | #' @export |
| 241 | auto_index <- function(x) { |
| 242 | x_rle <- rle(x) |
| 243 | index <- x_rle$lengths |
| 244 | names(index) <- x_rle$values |
| 245 | return(index) |
| 246 | } |
Hao Zhu | 72917f9 | 2019-03-15 18:41:42 -0400 | [diff] [blame] | 247 | |
| 248 | #' @rdname group_rows |
| 249 | #' @export |
| 250 | pack_rows <- group_rows |