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. |
bsalzer | 8593327 | 2018-02-12 17:26:46 -0500 | [diff] [blame] | 26 | #' @param colnum A numeric that determines how many columns the text should span. |
| 27 | #' The default setting will have the text span the entire length. |
georgegui | 4cc925b | 2018-03-01 12:02:45 -0600 | [diff] [blame] | 28 | #' @param bold A T/F value to control whether the text should be bolded. |
| 29 | #' @param italic A T/F value to control whether the text should to be emphasized. |
| 30 | #' @param hline_after A replicate of `hline.after` in xtable. It |
Hao Zhu | 6191e74 | 2018-03-01 13:09:08 -0500 | [diff] [blame] | 31 | #' addes a hline after the row |
georgegui | 4cc925b | 2018-03-01 12:02:45 -0600 | [diff] [blame] | 32 | #' @param extra_latex_after Extra LaTeX text to be added after the row. |
Hao Zhu | bd95bb2 | 2017-05-22 16:08:49 -0400 | [diff] [blame] | 33 | #' |
Hao Zhu | 78e6122 | 2017-05-24 20:53:35 -0400 | [diff] [blame] | 34 | #' @examples x <- knitr::kable(head(mtcars), "html") |
| 35 | #' # Put Row 2 to Row 5 into a Group and label it as "Group A" |
| 36 | #' group_rows(x, "Group A", 2, 5) |
| 37 | #' |
Hao Zhu | 62cdde5 | 2017-05-20 22:16:03 -0400 | [diff] [blame] | 38 | #' @export |
Hao Zhu | 49483bf | 2017-09-12 11:21:00 -0400 | [diff] [blame] | 39 | group_rows <- function(kable_input, group_label = NULL, |
| 40 | start_row = NULL, end_row = NULL, |
| 41 | index = NULL, |
Hao Zhu | d972e7f | 2017-05-22 13:27:15 -0400 | [diff] [blame] | 42 | label_row_css = "border-bottom: 1px solid;", |
Hao Zhu | 49483bf | 2017-09-12 11:21:00 -0400 | [diff] [blame] | 43 | latex_gap_space = "0.3em", |
georgegui | 4cc925b | 2018-03-01 12:02:45 -0600 | [diff] [blame] | 44 | escape = TRUE, latex_align = "l", colnum = NULL, |
| 45 | bold = T, |
| 46 | italic = F, |
| 47 | hline_after = F, |
| 48 | extra_latex_after = NULL) { |
Hao Zhu | 49483bf | 2017-09-12 11:21:00 -0400 | [diff] [blame] | 49 | |
Hao Zhu | 62cdde5 | 2017-05-20 22:16:03 -0400 | [diff] [blame] | 50 | kable_format <- attr(kable_input, "format") |
| 51 | if (!kable_format %in% c("html", "latex")) { |
Hao Zhu | 401ebd8 | 2018-01-14 17:10:20 -0500 | [diff] [blame] | 52 | warning("Please specify format in kable. kableExtra can customize either ", |
| 53 | "HTML or LaTeX outputs. See https://haozhu233.github.io/kableExtra/ ", |
| 54 | "for details.") |
Hao Zhu | 62cdde5 | 2017-05-20 22:16:03 -0400 | [diff] [blame] | 55 | return(kable_input) |
| 56 | } |
Hao Zhu | 49483bf | 2017-09-12 11:21:00 -0400 | [diff] [blame] | 57 | if (is.null(index)) { |
| 58 | if (kable_format == "html") { |
Salzer | 8f49eb6 | 2018-02-12 22:19:06 -0500 | [diff] [blame] | 59 | 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] | 60 | use label_row_css instead.") |
Hao Zhu | 49483bf | 2017-09-12 11:21:00 -0400 | [diff] [blame] | 61 | return(group_rows_html(kable_input, group_label, start_row, end_row, |
bsalzer | 8593327 | 2018-02-12 17:26:46 -0500 | [diff] [blame] | 62 | label_row_css, escape, colnum)) |
Hao Zhu | 49483bf | 2017-09-12 11:21:00 -0400 | [diff] [blame] | 63 | } |
| 64 | if (kable_format == "latex") { |
| 65 | return(group_rows_latex(kable_input, group_label, start_row, end_row, |
georgegui | 4cc925b | 2018-03-01 12:02:45 -0600 | [diff] [blame] | 66 | latex_gap_space, escape, latex_align, colnum, |
| 67 | bold, italic, hline_after, extra_latex_after)) |
Hao Zhu | 49483bf | 2017-09-12 11:21:00 -0400 | [diff] [blame] | 68 | } |
| 69 | } else { |
| 70 | index <- group_row_index_translator(index) |
| 71 | out <- kable_input |
| 72 | if (kable_format == "html") { |
| 73 | for (i in 1:nrow(index)) { |
Salzer | 8f49eb6 | 2018-02-12 22:19:06 -0500 | [diff] [blame] | 74 | 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] | 75 | use label_row_css instead.") |
Hao Zhu | 49483bf | 2017-09-12 11:21:00 -0400 | [diff] [blame] | 76 | out <- group_rows_html(out, index$header[i], |
| 77 | index$start[i], index$end[i], |
bsalzer | 8593327 | 2018-02-12 17:26:46 -0500 | [diff] [blame] | 78 | label_row_css, escape, colnum) |
Hao Zhu | 49483bf | 2017-09-12 11:21:00 -0400 | [diff] [blame] | 79 | } |
| 80 | } |
| 81 | if (kable_format == "latex") { |
| 82 | for (i in 1:nrow(index)) { |
| 83 | out <- group_rows_latex(out, index$header[i], |
| 84 | index$start[i], index$end[i], |
georgegui | 4cc925b | 2018-03-01 12:02:45 -0600 | [diff] [blame] | 85 | latex_gap_space, escape, latex_align, colnum, |
| 86 | bold, italic, hline_after, extra_latex_after) |
Hao Zhu | 49483bf | 2017-09-12 11:21:00 -0400 | [diff] [blame] | 87 | } |
| 88 | } |
| 89 | return(out) |
Hao Zhu | 62cdde5 | 2017-05-20 22:16:03 -0400 | [diff] [blame] | 90 | } |
Hao Zhu | 49483bf | 2017-09-12 11:21:00 -0400 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | group_row_index_translator <- function(index) { |
| 94 | index <- standardize_header_input(index) |
| 95 | index$start <- cumsum(c(1, index$colspan))[1:length(index$colspan)] |
| 96 | index$end <- cumsum(index$colspan) |
| 97 | index$header <- trimws(index$header) |
| 98 | index <- index[index$header != "", ] |
| 99 | return(index) |
Hao Zhu | 62cdde5 | 2017-05-20 22:16:03 -0400 | [diff] [blame] | 100 | } |
| 101 | |
Hao Zhu | d972e7f | 2017-05-22 13:27:15 -0400 | [diff] [blame] | 102 | group_rows_html <- function(kable_input, group_label, start_row, end_row, |
bsalzer | 8593327 | 2018-02-12 17:26:46 -0500 | [diff] [blame] | 103 | label_row_css, escape, colnum) { |
Hao Zhu | 62cdde5 | 2017-05-20 22:16:03 -0400 | [diff] [blame] | 104 | kable_attrs <- attributes(kable_input) |
Hao Zhu | 558c72f | 2017-07-24 15:12:00 -0400 | [diff] [blame] | 105 | kable_xml <- read_kable_as_xml(kable_input) |
Hao Zhu | 62cdde5 | 2017-05-20 22:16:03 -0400 | [diff] [blame] | 106 | kable_tbody <- xml_tpart(kable_xml, "tbody") |
| 107 | |
Hao Zhu | ac7e70f | 2017-08-02 00:18:36 -0400 | [diff] [blame] | 108 | if (escape) { |
| 109 | group_label <- escape_html(group_label) |
| 110 | } |
| 111 | |
Hao Zhu | 62cdde5 | 2017-05-20 22:16:03 -0400 | [diff] [blame] | 112 | group_header_rows <- attr(kable_input, "group_header_rows") |
| 113 | group_seq <- seq(start_row, end_row) |
| 114 | if (!is.null(group_header_rows)) { |
| 115 | group_seq <- positions_corrector(group_seq, group_header_rows, |
| 116 | length(xml_children(kable_tbody))) |
| 117 | } |
| 118 | |
| 119 | # Insert a group header row |
| 120 | starting_node <- xml_child(kable_tbody, group_seq[1]) |
bsalzer | 8593327 | 2018-02-12 17:26:46 -0500 | [diff] [blame] | 121 | kable_ncol <- ifelse(is.null(colnum), |
| 122 | length(xml_children(starting_node)), |
| 123 | colnum) |
Hao Zhu | 62cdde5 | 2017-05-20 22:16:03 -0400 | [diff] [blame] | 124 | group_header_row_text <- paste0( |
Hao Zhu | d972e7f | 2017-05-22 13:27:15 -0400 | [diff] [blame] | 125 | '<tr groupLength="', length(group_seq), '"><td colspan="', kable_ncol, |
| 126 | '" style="', label_row_css, '"><strong>', group_label, |
| 127 | "</strong></td></tr>" |
Hao Zhu | 62cdde5 | 2017-05-20 22:16:03 -0400 | [diff] [blame] | 128 | ) |
| 129 | group_header_row <- read_xml(group_header_row_text, options = "COMPACT") |
| 130 | xml_add_sibling(starting_node, group_header_row, .where = "before") |
| 131 | |
| 132 | # add indentations to items |
Hao Zhu | f2dfd14 | 2017-07-24 14:43:28 -0400 | [diff] [blame] | 133 | out <- as_kable_xml(kable_xml) |
Hao Zhu | 62cdde5 | 2017-05-20 22:16:03 -0400 | [diff] [blame] | 134 | attributes(out) <- kable_attrs |
| 135 | attr(out, "group_header_rows") <- c(attr(out, "group_header_rows"), group_seq[1]) |
Hao Zhu | 49483bf | 2017-09-12 11:21:00 -0400 | [diff] [blame] | 136 | out <- add_indent_html(out, positions = seq(start_row, end_row)) |
Hao Zhu | 62cdde5 | 2017-05-20 22:16:03 -0400 | [diff] [blame] | 137 | return(out) |
| 138 | } |
Hao Zhu | d972e7f | 2017-05-22 13:27:15 -0400 | [diff] [blame] | 139 | |
Hao Zhu | fc14c9b | 2017-05-22 14:03:22 -0400 | [diff] [blame] | 140 | group_rows_latex <- function(kable_input, group_label, start_row, end_row, |
georgegui | 4cc925b | 2018-03-01 12:02:45 -0600 | [diff] [blame] | 141 | gap_space, escape, latex_align, colnum, |
| 142 | bold = T, italic = F, hline_after = F, |
| 143 | extra_latex_after = NULL) { |
Hao Zhu | d972e7f | 2017-05-22 13:27:15 -0400 | [diff] [blame] | 144 | table_info <- magic_mirror(kable_input) |
Hao Zhu | d2c0f73 | 2017-08-26 10:40:14 -0400 | [diff] [blame] | 145 | out <- enc2utf8(as.character(kable_input)) |
Hao Zhu | d972e7f | 2017-05-22 13:27:15 -0400 | [diff] [blame] | 146 | |
Hao Zhu | 064990d | 2017-10-17 18:08:42 -0400 | [diff] [blame] | 147 | if (table_info$duplicated_rows) { |
| 148 | dup_fx_out <- fix_duplicated_rows_latex(out, table_info) |
| 149 | out <- dup_fx_out[[1]] |
| 150 | table_info <- dup_fx_out[[2]] |
| 151 | } |
| 152 | |
Hao Zhu | ac7e70f | 2017-08-02 00:18:36 -0400 | [diff] [blame] | 153 | if (escape) { |
| 154 | group_label <- escape_latex(group_label) |
| 155 | group_label <- gsub("\\\\", "\\\\\\\\", group_label) |
| 156 | } |
| 157 | |
georgegui | 4cc925b | 2018-03-01 12:02:45 -0600 | [diff] [blame] | 158 | if(bold){ |
| 159 | group_label <- paste0("\\\\textbf{", group_label, "}") |
| 160 | } |
| 161 | if(italic) group_label <- paste0("\\\\textit{", group_label, "}") |
Hao Zhu | d972e7f | 2017-05-22 13:27:15 -0400 | [diff] [blame] | 162 | # Add group label |
| 163 | rowtext <- table_info$contents[start_row + 1] |
| 164 | if (table_info$booktabs) { |
georgegui | 4cc925b | 2018-03-01 12:02:45 -0600 | [diff] [blame] | 165 | pre_rowtext <- paste0( |
Hao Zhu | d972e7f | 2017-05-22 13:27:15 -0400 | [diff] [blame] | 166 | "\\\\addlinespace[", gap_space, "]\n", |
bsalzer | 8593327 | 2018-02-12 17:26:46 -0500 | [diff] [blame] | 167 | "\\\\multicolumn{", ifelse(is.null(colnum), |
| 168 | table_info$ncol, |
| 169 | colnum), |
georgegui | 4cc925b | 2018-03-01 12:02:45 -0600 | [diff] [blame] | 170 | "}{", latex_align, "}{", group_label, |
| 171 | "}\\\\\\\\\n", ifelse(hline_after, "\\\\hline\n", '') |
Hao Zhu | d972e7f | 2017-05-22 13:27:15 -0400 | [diff] [blame] | 172 | ) |
Hao Zhu | d972e7f | 2017-05-22 13:27:15 -0400 | [diff] [blame] | 173 | } else { |
| 174 | rowtext <- paste0("\\\\hline\n", rowtext) |
georgegui | 4cc925b | 2018-03-01 12:02:45 -0600 | [diff] [blame] | 175 | pre_rowtext <- paste0( |
| 176 | "\\\\hline\n\\\\multicolumn{", table_info$ncol, "}{", latex_align,"}{", |
| 177 | group_label, "}\\\\\\\\\n" |
Hao Zhu | d972e7f | 2017-05-22 13:27:15 -0400 | [diff] [blame] | 178 | ) |
| 179 | } |
georgegui | 4cc925b | 2018-03-01 12:02:45 -0600 | [diff] [blame] | 180 | if(!is.null(extra_latex_after)){ |
| 181 | pre_rowtext <- paste0(pre_rowtext, |
| 182 | regex_escape(extra_latex_after, double_backslash = TRUE)) |
| 183 | } |
| 184 | new_rowtext <- paste0(pre_rowtext, rowtext) |
Hao Zhu | d972e7f | 2017-05-22 13:27:15 -0400 | [diff] [blame] | 185 | out <- sub(rowtext, new_rowtext, out) |
Hao Zhu | 8f20299 | 2017-07-15 02:20:18 -0400 | [diff] [blame] | 186 | out <- gsub("\\\\addlinespace\n", "", out) |
Hao Zhu | d2c0f73 | 2017-08-26 10:40:14 -0400 | [diff] [blame] | 187 | out <- structure(out, format = "latex", class = "knitr_kable") |
Hao Zhu | 32f43f7 | 2017-06-20 18:24:54 -0400 | [diff] [blame] | 188 | table_info$group_rows_used <- TRUE |
| 189 | attr(out, "kable_meta") <- table_info |
Hao Zhu | 49483bf | 2017-09-12 11:21:00 -0400 | [diff] [blame] | 190 | out <- add_indent_latex(out, seq(start_row, end_row)) |
Hao Zhu | d972e7f | 2017-05-22 13:27:15 -0400 | [diff] [blame] | 191 | return(out) |
| 192 | } |