blob: 746fb1aa56ce23db163c01c2d756d26044845873 [file] [log] [blame]
Hao Zhu62cdde52017-05-20 22:16:03 -04001#' Put a few rows of a table into one category
2#'
Hao Zhubd95bb22017-05-22 16:08:49 -04003#' @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 Zhu49483bf2017-09-12 11:21:00 -040012#' @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 Zhubd95bb22017-05-22 16:08:49 -040014#' @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 Zhuac7e70f2017-08-02 00:18:36 -040020#' @param escape A T/F value showing whether special characters should be
21#' escaped.
Salzer8f49eb62018-02-12 22:19:06 -050022#' @param latex_align Adjust justification of group_label in latex only. Value should be "c" for
Salzer15550852018-02-12 22:09:14 -050023#' 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 Zhu779569e2019-04-26 17:06:33 -040026#' @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.
bsalzer85933272018-02-12 17:26:46 -050029#' @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.
georgegui4cc925b2018-03-01 12:02:45 -060031#' @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 Salzera3f43ad2018-03-04 15:03:20 -050033#' @param hline_before A T/F value that addes a horizontal line before the group_row label. Default
34#' value is False.
georgegui4cc925b2018-03-01 12:02:45 -060035#' @param hline_after A replicate of `hline.after` in xtable. It
Hao Zhu6191e742018-03-01 13:09:08 -050036#' addes a hline after the row
georgegui4cc925b2018-03-01 12:02:45 -060037#' @param extra_latex_after Extra LaTeX text to be added after the row.
Hao Zhuebdb3c22020-08-12 08:27:38 -040038#' @param indent A T/F value to control whether list items are indented.
Hao Zhubd95bb22017-05-22 16:08:49 -040039#'
Hao Zhu78e61222017-05-24 20:53:35 -040040#' @examples x <- knitr::kable(head(mtcars), "html")
41#' # Put Row 2 to Row 5 into a Group and label it as "Group A"
Hao Zhu72917f92019-03-15 18:41:42 -040042#' pack_rows(x, "Group A", 2, 5)
Hao Zhu78e61222017-05-24 20:53:35 -040043#'
Hao Zhu62cdde52017-05-20 22:16:03 -040044#' @export
Hao Zhu49483bf2017-09-12 11:21:00 -040045group_rows <- function(kable_input, group_label = NULL,
46 start_row = NULL, end_row = NULL,
47 index = NULL,
Hao Zhud972e7f2017-05-22 13:27:15 -040048 label_row_css = "border-bottom: 1px solid;",
Hao Zhu49483bf2017-09-12 11:21:00 -040049 latex_gap_space = "0.3em",
Hao Zhu779569e2019-04-26 17:06:33 -040050 escape = TRUE, latex_align = "l",
51 latex_wrap_text = FALSE,
52 colnum = NULL,
Hao Zhu2742ffc2018-10-17 11:23:44 -040053 bold = TRUE,
54 italic = FALSE,
55 hline_before = FALSE,
56 hline_after = FALSE,
57 extra_latex_after = NULL,
58 indent = TRUE) {
Hao Zhu49483bf2017-09-12 11:21:00 -040059
Hao Zhu62cdde52017-05-20 22:16:03 -040060 kable_format <- attr(kable_input, "format")
61 if (!kable_format %in% c("html", "latex")) {
Hao Zhu401ebd82018-01-14 17:10:20 -050062 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 Zhu62cdde52017-05-20 22:16:03 -040065 return(kable_input)
66 }
Adrien Le Guilloufdffec62019-09-04 00:03:52 +020067
Hao Zhu49483bf2017-09-12 11:21:00 -040068 if (is.null(index)) {
69 if (kable_format == "html") {
Hao Zhu779569e2019-04-26 17:06:33 -040070 if (!missing(latex_align)) warning("latex_align parameter is not used in HTML Mode,
Salzer15550852018-02-12 22:09:14 -050071 use label_row_css instead.")
Hao Zhu49483bf2017-09-12 11:21:00 -040072 return(group_rows_html(kable_input, group_label, start_row, end_row,
Adrien Le Guilloufdffec62019-09-04 00:03:52 +020073 label_row_css, escape, colnum, indent,
74 bold, italic))
Hao Zhu49483bf2017-09-12 11:21:00 -040075 }
76 if (kable_format == "latex") {
77 return(group_rows_latex(kable_input, group_label, start_row, end_row,
georgegui4cc925b2018-03-01 12:02:45 -060078 latex_gap_space, escape, latex_align, colnum,
Hao Zhu2742ffc2018-10-17 11:23:44 -040079 bold, italic, hline_before, hline_after,
Hao Zhu779569e2019-04-26 17:06:33 -040080 extra_latex_after, indent, latex_wrap_text))
Hao Zhu49483bf2017-09-12 11:21:00 -040081 }
82 } else {
83 index <- group_row_index_translator(index)
84 out <- kable_input
85 if (kable_format == "html") {
86 for (i in 1:nrow(index)) {
Hao Zhu779569e2019-04-26 17:06:33 -040087 if (!missing(latex_align)) warning("latex_align parameter is not used in HTML Mode,
Salzer15550852018-02-12 22:09:14 -050088 use label_row_css instead.")
Hao Zhu49483bf2017-09-12 11:21:00 -040089 out <- group_rows_html(out, index$header[i],
90 index$start[i], index$end[i],
Adrien Le Guilloufdffec62019-09-04 00:03:52 +020091 label_row_css, escape, colnum, indent,
92 bold, italic)
Hao Zhu49483bf2017-09-12 11:21:00 -040093 }
94 }
95 if (kable_format == "latex") {
96 for (i in 1:nrow(index)) {
97 out <- group_rows_latex(out, index$header[i],
98 index$start[i], index$end[i],
georgegui4cc925b2018-03-01 12:02:45 -060099 latex_gap_space, escape, latex_align, colnum,
Hao Zhu2742ffc2018-10-17 11:23:44 -0400100 bold, italic, hline_before, hline_after,
Hao Zhu779569e2019-04-26 17:06:33 -0400101 extra_latex_after, indent, latex_wrap_text)
Hao Zhu49483bf2017-09-12 11:21:00 -0400102 }
103 }
104 return(out)
Hao Zhu62cdde52017-05-20 22:16:03 -0400105 }
Hao Zhu49483bf2017-09-12 11:21:00 -0400106}
107
108group_row_index_translator <- function(index) {
109 index <- standardize_header_input(index)
110 index$start <- cumsum(c(1, index$colspan))[1:length(index$colspan)]
111 index$end <- cumsum(index$colspan)
112 index$header <- trimws(index$header)
113 index <- index[index$header != "", ]
114 return(index)
Hao Zhu62cdde52017-05-20 22:16:03 -0400115}
116
Hao Zhud972e7f2017-05-22 13:27:15 -0400117group_rows_html <- function(kable_input, group_label, start_row, end_row,
Hao Zhucedf90e2020-08-12 08:45:34 -0400118 label_row_css, escape, colnum, indent,
119 bold = TRUE, italic = FALSE) {
Hao Zhu62cdde52017-05-20 22:16:03 -0400120 kable_attrs <- attributes(kable_input)
Hao Zhu558c72f2017-07-24 15:12:00 -0400121 kable_xml <- read_kable_as_xml(kable_input)
Hao Zhu62cdde52017-05-20 22:16:03 -0400122 kable_tbody <- xml_tpart(kable_xml, "tbody")
123
Hao Zhuac7e70f2017-08-02 00:18:36 -0400124 if (escape) {
125 group_label <- escape_html(group_label)
126 }
127
Hao Zhu62cdde52017-05-20 22:16:03 -0400128 group_header_rows <- attr(kable_input, "group_header_rows")
129 group_seq <- seq(start_row, end_row)
130 if (!is.null(group_header_rows)) {
131 group_seq <- positions_corrector(group_seq, group_header_rows,
132 length(xml_children(kable_tbody)))
jokorn69c8bd52019-06-29 11:11:11 +0200133 # Update the old group_header_rows attribute with their new positions
134 kable_attrs$group_header_rows <- ifelse(kable_attrs$group_header_rows > group_seq[1],
135 kable_attrs$group_header_rows+1,
136 kable_attrs$group_header_rows)
Hao Zhu62cdde52017-05-20 22:16:03 -0400137 }
138
139 # Insert a group header row
140 starting_node <- xml_child(kable_tbody, group_seq[1])
bsalzer85933272018-02-12 17:26:46 -0500141 kable_ncol <- ifelse(is.null(colnum),
142 length(xml_children(starting_node)),
143 colnum)
Adrien Le Guilloufdffec62019-09-04 00:03:52 +0200144
145 if (bold) group_label <- paste0("<strong>", group_label, "</strong>")
146 if (italic) group_label <- paste0("<em>", group_label, "</em>")
147
Hao Zhu8b16a6c2020-08-18 16:59:20 -0400148 if (label_row_css == "border-bottom: 1px solid;") {
149 if (!is.null(attr(kable_input, "lightable_class"))) {
150 lightable_class <- attr(kable_input, "lightable_class")
151 if (lightable_class %in% c(
152 "lightable-classic", "lightable-classic-2", "lightable-minimal")) {
153 label_row_css <- "border-bottom: 0;"
154 }
155 if (lightable_class %in% c("lightable-paper")) {
156 label_row_css <- "border-bottom: 1px solid #00000020;"
157 }
158 if (lightable_class %in% c("lightable-material")) {
159 label_row_css <- "border-bottom: 1px solid #eee; "
160 }
161 if (lightable_class %in% c("lightable-material-dark")) {
162 label_row_css <- "border-bottom: 1px solid #FFFFFF12; color: #FFFFFF60;"
163 }
164 }
165 }
166
Hao Zhu62cdde52017-05-20 22:16:03 -0400167 group_header_row_text <- paste0(
Hao Zhud972e7f2017-05-22 13:27:15 -0400168 '<tr groupLength="', length(group_seq), '"><td colspan="', kable_ncol,
Adrien Le Guilloufdffec62019-09-04 00:03:52 +0200169 '" style="', label_row_css, '">', group_label, "</td></tr>")
170
Hao Zhu62cdde52017-05-20 22:16:03 -0400171 group_header_row <- read_xml(group_header_row_text, options = "COMPACT")
172 xml_add_sibling(starting_node, group_header_row, .where = "before")
173
174 # add indentations to items
Hao Zhuf2dfd142017-07-24 14:43:28 -0400175 out <- as_kable_xml(kable_xml)
Hao Zhu62cdde52017-05-20 22:16:03 -0400176 attributes(out) <- kable_attrs
177 attr(out, "group_header_rows") <- c(attr(out, "group_header_rows"), group_seq[1])
Hao Zhu2742ffc2018-10-17 11:23:44 -0400178 if (indent) {
179 out <- add_indent_html(out, positions = seq(start_row, end_row))
180 }
Hao Zhu62cdde52017-05-20 22:16:03 -0400181 return(out)
182}
Hao Zhud972e7f2017-05-22 13:27:15 -0400183
Hao Zhufc14c9b2017-05-22 14:03:22 -0400184group_rows_latex <- function(kable_input, group_label, start_row, end_row,
georgegui4cc925b2018-03-01 12:02:45 -0600185 gap_space, escape, latex_align, colnum,
Hao Zhu779569e2019-04-26 17:06:33 -0400186 bold = T, italic = F, hline_before = F, hline_after = F,
187 extra_latex_after = NULL, indent, latex_wrap_text = F) {
Hao Zhud972e7f2017-05-22 13:27:15 -0400188 table_info <- magic_mirror(kable_input)
Hao Zhu3fc0e882018-04-03 16:06:41 -0400189 out <- solve_enc(kable_input)
Hao Zhud972e7f2017-05-22 13:27:15 -0400190
Hao Zhu064990d2017-10-17 18:08:42 -0400191 if (table_info$duplicated_rows) {
192 dup_fx_out <- fix_duplicated_rows_latex(out, table_info)
193 out <- dup_fx_out[[1]]
194 table_info <- dup_fx_out[[2]]
195 }
196
Hao Zhuac7e70f2017-08-02 00:18:36 -0400197 if (escape) {
Hao Zhuf94a26f2018-04-05 17:42:55 -0400198 group_label <- input_escape(group_label, latex_align)
Hao Zhuac7e70f2017-08-02 00:18:36 -0400199 }
200
Hao Zhuf94a26f2018-04-05 17:42:55 -0400201 if (bold) {
georgegui4cc925b2018-03-01 12:02:45 -0600202 group_label <- paste0("\\\\textbf{", group_label, "}")
203 }
Hao Zhu779569e2019-04-26 17:06:33 -0400204
Hao Zhuf94a26f2018-04-05 17:42:55 -0400205 if (italic) group_label <- paste0("\\\\textit{", group_label, "}")
Hao Zhud972e7f2017-05-22 13:27:15 -0400206 # Add group label
Hao Zhu779569e2019-04-26 17:06:33 -0400207 if (latex_wrap_text) {
208 latex_align <- switch(
209 latex_align,
210 "l" = "p{\\\\linewidth}",
211 "c" = ">{\\\\centering\\\\arraybackslash}p{\\\\linewidth}",
212 "r" = ">{\\\\centering\\\\arraybackslash}p{\\\\linewidth}"
213 )
214 }
215
216
Hao Zhu334376d2020-08-19 00:45:09 -0400217 rowtext <- table_info$contents[start_row + table_info$position_offset]
Hao Zhud972e7f2017-05-22 13:27:15 -0400218 if (table_info$booktabs) {
Hao Zhu334376d2020-08-19 00:45:09 -0400219 pre_rowtext <- paste0("\\\\addlinespace[", gap_space, "]\n")
Hao Zhud972e7f2017-05-22 13:27:15 -0400220 } else {
Hao Zhu334376d2020-08-19 00:45:09 -0400221 pre_rowtext <- ''
222 hline_after <- TRUE
Hao Zhud972e7f2017-05-22 13:27:15 -0400223 }
Hao Zhu334376d2020-08-19 00:45:09 -0400224 pre_rowtext <- paste0(
225 pre_rowtext,
226 ifelse(hline_before,"\\\\hline\n", ""),
227 "\\\\multicolumn{", ifelse(is.null(colnum),
228 table_info$ncol,
229 colnum),
230 "}{", latex_align,"}{", group_label,
231 "}\\\\\\\\\n", ifelse(hline_after, "\\\\hline\n", '')
232 )
georgegui4cc925b2018-03-01 12:02:45 -0600233 if(!is.null(extra_latex_after)){
234 pre_rowtext <- paste0(pre_rowtext,
235 regex_escape(extra_latex_after, double_backslash = TRUE))
236 }
237 new_rowtext <- paste0(pre_rowtext, rowtext)
Hao Zhu9c3007e2020-08-03 13:52:38 -0400238 if (start_row + 1 == table_info$nrow &
Hao Zhu334376d2020-08-19 00:45:09 -0400239 !is.null(table_info$repeat_header_latex) & table_info$booktabs) {
Hao Zhu9c3007e2020-08-03 13:52:38 -0400240 out <- sub(paste0(rowtext, "\\\\\\\\\\*\n"),
241 paste0(new_rowtext, "\\\\\\\\\\*\n"),
242 out)
243 } else {
244 out <- sub(paste0(rowtext, "\\\\\\\\\n"),
245 paste0(new_rowtext, "\\\\\\\\\n"),
246 out)
247 }
248
Hao Zhu8f202992017-07-15 02:20:18 -0400249 out <- gsub("\\\\addlinespace\n", "", out)
Hao Zhud2c0f732017-08-26 10:40:14 -0400250 out <- structure(out, format = "latex", class = "knitr_kable")
Hao Zhu32f43f72017-06-20 18:24:54 -0400251 table_info$group_rows_used <- TRUE
252 attr(out, "kable_meta") <- table_info
Hao Zhu2742ffc2018-10-17 11:23:44 -0400253 if (indent) {
254 out <- add_indent_latex(out, seq(start_row, end_row))
255 }
Hao Zhud972e7f2017-05-22 13:27:15 -0400256 return(out)
257}
Hao Zhube853f72018-05-20 18:52:26 -0400258
259#' Automatically figuring out the group_row index
260#'
261#' @description This helper function allows users to build the `group_row`
262#' index more quickly and use `group_rows` in a way that is similar with
263#' `collapse_rows`.
264#'
265#' @param x The index column. A vector. For example `c("a", "a", "b", "b", "b")``
266#'
267#' @export
268auto_index <- function(x) {
269 x_rle <- rle(x)
270 index <- x_rle$lengths
271 names(index) <- x_rle$values
272 return(index)
273}
Hao Zhu72917f92019-03-15 18:41:42 -0400274
275#' @rdname group_rows
276#' @export
277pack_rows <- group_rows