blob: 2684782dc1172ba170cd0fd15ba2a5c091ff60fe [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.
Salzer15550852018-02-12 22:09:14 -050022#' @param align Adjust justification of group_label in latex only. Value should be "c" for
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.
bsalzer85933272018-02-12 17:26:46 -050026#' @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.
Hao Zhubd95bb22017-05-22 16:08:49 -040028#'
Hao Zhu78e61222017-05-24 20:53:35 -040029#' @examples x <- knitr::kable(head(mtcars), "html")
30#' # Put Row 2 to Row 5 into a Group and label it as "Group A"
31#' group_rows(x, "Group A", 2, 5)
32#'
Hao Zhu62cdde52017-05-20 22:16:03 -040033#' @export
Hao Zhu49483bf2017-09-12 11:21:00 -040034group_rows <- function(kable_input, group_label = NULL,
35 start_row = NULL, end_row = NULL,
36 index = NULL,
Hao Zhud972e7f2017-05-22 13:27:15 -040037 label_row_css = "border-bottom: 1px solid;",
Hao Zhu49483bf2017-09-12 11:21:00 -040038 latex_gap_space = "0.3em",
Salzer15550852018-02-12 22:09:14 -050039 escape = TRUE, align = "l", colnum = NULL) {
Hao Zhu49483bf2017-09-12 11:21:00 -040040
Hao Zhu62cdde52017-05-20 22:16:03 -040041 kable_format <- attr(kable_input, "format")
42 if (!kable_format %in% c("html", "latex")) {
Hao Zhu401ebd82018-01-14 17:10:20 -050043 warning("Please specify format in kable. kableExtra can customize either ",
44 "HTML or LaTeX outputs. See https://haozhu233.github.io/kableExtra/ ",
45 "for details.")
Hao Zhu62cdde52017-05-20 22:16:03 -040046 return(kable_input)
47 }
Hao Zhu49483bf2017-09-12 11:21:00 -040048 if (is.null(index)) {
49 if (kable_format == "html") {
Salzer15550852018-02-12 22:09:14 -050050 if(!missing(align)) warning("Align parameter is not used in HTML Mode,
51 use label_row_css instead.")
Hao Zhu49483bf2017-09-12 11:21:00 -040052 return(group_rows_html(kable_input, group_label, start_row, end_row,
bsalzer85933272018-02-12 17:26:46 -050053 label_row_css, escape, colnum))
Hao Zhu49483bf2017-09-12 11:21:00 -040054 }
55 if (kable_format == "latex") {
56 return(group_rows_latex(kable_input, group_label, start_row, end_row,
Salzer15550852018-02-12 22:09:14 -050057 latex_gap_space, escape, align, colnum))
Hao Zhu49483bf2017-09-12 11:21:00 -040058 }
59 } else {
60 index <- group_row_index_translator(index)
61 out <- kable_input
62 if (kable_format == "html") {
63 for (i in 1:nrow(index)) {
Salzer15550852018-02-12 22:09:14 -050064 if(!missing(align)) warning("Align parameter is not used in HTML Mode,
65 use label_row_css instead.")
Hao Zhu49483bf2017-09-12 11:21:00 -040066 out <- group_rows_html(out, index$header[i],
67 index$start[i], index$end[i],
bsalzer85933272018-02-12 17:26:46 -050068 label_row_css, escape, colnum)
Hao Zhu49483bf2017-09-12 11:21:00 -040069 }
70 }
71 if (kable_format == "latex") {
72 for (i in 1:nrow(index)) {
73 out <- group_rows_latex(out, index$header[i],
74 index$start[i], index$end[i],
Salzer15550852018-02-12 22:09:14 -050075 latex_gap_space, escape, align, colnum)
Hao Zhu49483bf2017-09-12 11:21:00 -040076 }
77 }
78 return(out)
Hao Zhu62cdde52017-05-20 22:16:03 -040079 }
Hao Zhu49483bf2017-09-12 11:21:00 -040080}
81
82group_row_index_translator <- function(index) {
83 index <- standardize_header_input(index)
84 index$start <- cumsum(c(1, index$colspan))[1:length(index$colspan)]
85 index$end <- cumsum(index$colspan)
86 index$header <- trimws(index$header)
87 index <- index[index$header != "", ]
88 return(index)
Hao Zhu62cdde52017-05-20 22:16:03 -040089}
90
Hao Zhud972e7f2017-05-22 13:27:15 -040091group_rows_html <- function(kable_input, group_label, start_row, end_row,
bsalzer85933272018-02-12 17:26:46 -050092 label_row_css, escape, colnum) {
Hao Zhu62cdde52017-05-20 22:16:03 -040093 kable_attrs <- attributes(kable_input)
Hao Zhu558c72f2017-07-24 15:12:00 -040094 kable_xml <- read_kable_as_xml(kable_input)
Hao Zhu62cdde52017-05-20 22:16:03 -040095 kable_tbody <- xml_tpart(kable_xml, "tbody")
96
Hao Zhuac7e70f2017-08-02 00:18:36 -040097 if (escape) {
98 group_label <- escape_html(group_label)
99 }
100
Hao Zhu62cdde52017-05-20 22:16:03 -0400101 group_header_rows <- attr(kable_input, "group_header_rows")
102 group_seq <- seq(start_row, end_row)
103 if (!is.null(group_header_rows)) {
104 group_seq <- positions_corrector(group_seq, group_header_rows,
105 length(xml_children(kable_tbody)))
106 }
107
108 # Insert a group header row
109 starting_node <- xml_child(kable_tbody, group_seq[1])
bsalzer85933272018-02-12 17:26:46 -0500110 kable_ncol <- ifelse(is.null(colnum),
111 length(xml_children(starting_node)),
112 colnum)
Hao Zhu62cdde52017-05-20 22:16:03 -0400113 group_header_row_text <- paste0(
Hao Zhud972e7f2017-05-22 13:27:15 -0400114 '<tr groupLength="', length(group_seq), '"><td colspan="', kable_ncol,
115 '" style="', label_row_css, '"><strong>', group_label,
116 "</strong></td></tr>"
Hao Zhu62cdde52017-05-20 22:16:03 -0400117 )
118 group_header_row <- read_xml(group_header_row_text, options = "COMPACT")
119 xml_add_sibling(starting_node, group_header_row, .where = "before")
120
121 # add indentations to items
Hao Zhuf2dfd142017-07-24 14:43:28 -0400122 out <- as_kable_xml(kable_xml)
Hao Zhu62cdde52017-05-20 22:16:03 -0400123 attributes(out) <- kable_attrs
124 attr(out, "group_header_rows") <- c(attr(out, "group_header_rows"), group_seq[1])
Hao Zhu49483bf2017-09-12 11:21:00 -0400125 out <- add_indent_html(out, positions = seq(start_row, end_row))
Hao Zhu62cdde52017-05-20 22:16:03 -0400126 return(out)
127}
Hao Zhud972e7f2017-05-22 13:27:15 -0400128
Hao Zhufc14c9b2017-05-22 14:03:22 -0400129group_rows_latex <- function(kable_input, group_label, start_row, end_row,
Salzer15550852018-02-12 22:09:14 -0500130 gap_space, escape, align, colnum) {
Hao Zhud972e7f2017-05-22 13:27:15 -0400131 table_info <- magic_mirror(kable_input)
Hao Zhud2c0f732017-08-26 10:40:14 -0400132 out <- enc2utf8(as.character(kable_input))
Hao Zhud972e7f2017-05-22 13:27:15 -0400133
Hao Zhu064990d2017-10-17 18:08:42 -0400134 if (table_info$duplicated_rows) {
135 dup_fx_out <- fix_duplicated_rows_latex(out, table_info)
136 out <- dup_fx_out[[1]]
137 table_info <- dup_fx_out[[2]]
138 }
139
Hao Zhuac7e70f2017-08-02 00:18:36 -0400140 if (escape) {
141 group_label <- escape_latex(group_label)
142 group_label <- gsub("\\\\", "\\\\\\\\", group_label)
143 }
144
Hao Zhud972e7f2017-05-22 13:27:15 -0400145 # Add group label
146 rowtext <- table_info$contents[start_row + 1]
147 if (table_info$booktabs) {
148 new_rowtext <- paste0(
149 "\\\\addlinespace[", gap_space, "]\n",
bsalzer85933272018-02-12 17:26:46 -0500150 "\\\\multicolumn{", ifelse(is.null(colnum),
151 table_info$ncol,
152 colnum),
Salzer15550852018-02-12 22:09:14 -0500153 "}{", align, "}{\\\\textbf{", group_label,
Hao Zhud972e7f2017-05-22 13:27:15 -0400154 "}}\\\\\\\\\n",
155 rowtext
156 )
Hao Zhud972e7f2017-05-22 13:27:15 -0400157 } else {
158 rowtext <- paste0("\\\\hline\n", rowtext)
159 new_rowtext <- paste0(
Salzer15550852018-02-12 22:09:14 -0500160 "\\\\hline\n\\\\multicolumn{", table_info$ncol, "}{", align,"}{\\\\textbf{",
Hao Zhud972e7f2017-05-22 13:27:15 -0400161 group_label, "}}\\\\\\\\\n", rowtext
162 )
163 }
164 out <- sub(rowtext, new_rowtext, out)
Hao Zhu8f202992017-07-15 02:20:18 -0400165 out <- gsub("\\\\addlinespace\n", "", out)
Hao Zhud2c0f732017-08-26 10:40:14 -0400166 out <- structure(out, format = "latex", class = "knitr_kable")
Hao Zhu32f43f72017-06-20 18:24:54 -0400167 table_info$group_rows_used <- TRUE
168 attr(out, "kable_meta") <- table_info
Hao Zhu49483bf2017-09-12 11:21:00 -0400169 out <- add_indent_latex(out, seq(start_row, end_row))
Hao Zhud972e7f2017-05-22 13:27:15 -0400170 return(out)
171}