blob: 43521055cdf7cd76cf69637d4d2c819ae6a54bcf [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.
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.
georgegui4cc925b2018-03-01 12:02:45 -060028#' @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.
Brian Salzera3f43ad2018-03-04 15:03:20 -050030#' @param hline_before A T/F value that addes a horizontal line before the group_row label. Default
31#' value is False.
georgegui4cc925b2018-03-01 12:02:45 -060032#' @param hline_after A replicate of `hline.after` in xtable. It
Hao Zhu6191e742018-03-01 13:09:08 -050033#' addes a hline after the row
georgegui4cc925b2018-03-01 12:02:45 -060034#' @param extra_latex_after Extra LaTeX text to be added after the row.
Hao Zhubd95bb22017-05-22 16:08:49 -040035#'
Hao Zhu78e61222017-05-24 20:53:35 -040036#' @examples x <- knitr::kable(head(mtcars), "html")
37#' # Put Row 2 to Row 5 into a Group and label it as "Group A"
38#' group_rows(x, "Group A", 2, 5)
39#'
Hao Zhu62cdde52017-05-20 22:16:03 -040040#' @export
Hao Zhu49483bf2017-09-12 11:21:00 -040041group_rows <- function(kable_input, group_label = NULL,
42 start_row = NULL, end_row = NULL,
43 index = NULL,
Hao Zhud972e7f2017-05-22 13:27:15 -040044 label_row_css = "border-bottom: 1px solid;",
Hao Zhu49483bf2017-09-12 11:21:00 -040045 latex_gap_space = "0.3em",
georgegui4cc925b2018-03-01 12:02:45 -060046 escape = TRUE, latex_align = "l", colnum = NULL,
47 bold = T,
48 italic = F,
Brian Salzera3f43ad2018-03-04 15:03:20 -050049 hline_before = F,
georgegui4cc925b2018-03-01 12:02:45 -060050 hline_after = F,
51 extra_latex_after = NULL) {
Hao Zhu49483bf2017-09-12 11:21:00 -040052
Hao Zhu62cdde52017-05-20 22:16:03 -040053 kable_format <- attr(kable_input, "format")
54 if (!kable_format %in% c("html", "latex")) {
Hao Zhu401ebd82018-01-14 17:10:20 -050055 warning("Please specify format in kable. kableExtra can customize either ",
56 "HTML or LaTeX outputs. See https://haozhu233.github.io/kableExtra/ ",
57 "for details.")
Hao Zhu62cdde52017-05-20 22:16:03 -040058 return(kable_input)
59 }
Hao Zhu49483bf2017-09-12 11:21:00 -040060 if (is.null(index)) {
61 if (kable_format == "html") {
Salzer8f49eb62018-02-12 22:19:06 -050062 if(!missing(latex_align)) warning("latex_align parameter is not used in HTML Mode,
Salzer15550852018-02-12 22:09:14 -050063 use label_row_css instead.")
Hao Zhu49483bf2017-09-12 11:21:00 -040064 return(group_rows_html(kable_input, group_label, start_row, end_row,
bsalzer85933272018-02-12 17:26:46 -050065 label_row_css, escape, colnum))
Hao Zhu49483bf2017-09-12 11:21:00 -040066 }
67 if (kable_format == "latex") {
68 return(group_rows_latex(kable_input, group_label, start_row, end_row,
georgegui4cc925b2018-03-01 12:02:45 -060069 latex_gap_space, escape, latex_align, colnum,
Brian Salzera3f43ad2018-03-04 15:03:20 -050070 bold, italic, hline_before, hline_after, extra_latex_after))
Hao Zhu49483bf2017-09-12 11:21:00 -040071 }
72 } else {
73 index <- group_row_index_translator(index)
74 out <- kable_input
75 if (kable_format == "html") {
76 for (i in 1:nrow(index)) {
Salzer8f49eb62018-02-12 22:19:06 -050077 if(!missing(latex_align)) warning("latex_align parameter is not used in HTML Mode,
Salzer15550852018-02-12 22:09:14 -050078 use label_row_css instead.")
Hao Zhu49483bf2017-09-12 11:21:00 -040079 out <- group_rows_html(out, index$header[i],
80 index$start[i], index$end[i],
bsalzer85933272018-02-12 17:26:46 -050081 label_row_css, escape, colnum)
Hao Zhu49483bf2017-09-12 11:21:00 -040082 }
83 }
84 if (kable_format == "latex") {
85 for (i in 1:nrow(index)) {
86 out <- group_rows_latex(out, index$header[i],
87 index$start[i], index$end[i],
georgegui4cc925b2018-03-01 12:02:45 -060088 latex_gap_space, escape, latex_align, colnum,
Brian Salzera3f43ad2018-03-04 15:03:20 -050089 bold, italic, hline_before, hline_after, extra_latex_after)
Hao Zhu49483bf2017-09-12 11:21:00 -040090 }
91 }
92 return(out)
Hao Zhu62cdde52017-05-20 22:16:03 -040093 }
Hao Zhu49483bf2017-09-12 11:21:00 -040094}
95
96group_row_index_translator <- function(index) {
97 index <- standardize_header_input(index)
98 index$start <- cumsum(c(1, index$colspan))[1:length(index$colspan)]
99 index$end <- cumsum(index$colspan)
100 index$header <- trimws(index$header)
101 index <- index[index$header != "", ]
102 return(index)
Hao Zhu62cdde52017-05-20 22:16:03 -0400103}
104
Hao Zhud972e7f2017-05-22 13:27:15 -0400105group_rows_html <- function(kable_input, group_label, start_row, end_row,
bsalzer85933272018-02-12 17:26:46 -0500106 label_row_css, escape, colnum) {
Hao Zhu62cdde52017-05-20 22:16:03 -0400107 kable_attrs <- attributes(kable_input)
Hao Zhu558c72f2017-07-24 15:12:00 -0400108 kable_xml <- read_kable_as_xml(kable_input)
Hao Zhu62cdde52017-05-20 22:16:03 -0400109 kable_tbody <- xml_tpart(kable_xml, "tbody")
110
Hao Zhuac7e70f2017-08-02 00:18:36 -0400111 if (escape) {
112 group_label <- escape_html(group_label)
113 }
114
Hao Zhu62cdde52017-05-20 22:16:03 -0400115 group_header_rows <- attr(kable_input, "group_header_rows")
116 group_seq <- seq(start_row, end_row)
117 if (!is.null(group_header_rows)) {
118 group_seq <- positions_corrector(group_seq, group_header_rows,
119 length(xml_children(kable_tbody)))
120 }
121
122 # Insert a group header row
123 starting_node <- xml_child(kable_tbody, group_seq[1])
bsalzer85933272018-02-12 17:26:46 -0500124 kable_ncol <- ifelse(is.null(colnum),
125 length(xml_children(starting_node)),
126 colnum)
Hao Zhu62cdde52017-05-20 22:16:03 -0400127 group_header_row_text <- paste0(
Hao Zhud972e7f2017-05-22 13:27:15 -0400128 '<tr groupLength="', length(group_seq), '"><td colspan="', kable_ncol,
129 '" style="', label_row_css, '"><strong>', group_label,
130 "</strong></td></tr>"
Hao Zhu62cdde52017-05-20 22:16:03 -0400131 )
132 group_header_row <- read_xml(group_header_row_text, options = "COMPACT")
133 xml_add_sibling(starting_node, group_header_row, .where = "before")
134
135 # add indentations to items
Hao Zhuf2dfd142017-07-24 14:43:28 -0400136 out <- as_kable_xml(kable_xml)
Hao Zhu62cdde52017-05-20 22:16:03 -0400137 attributes(out) <- kable_attrs
138 attr(out, "group_header_rows") <- c(attr(out, "group_header_rows"), group_seq[1])
Hao Zhu49483bf2017-09-12 11:21:00 -0400139 out <- add_indent_html(out, positions = seq(start_row, end_row))
Hao Zhu62cdde52017-05-20 22:16:03 -0400140 return(out)
141}
Hao Zhud972e7f2017-05-22 13:27:15 -0400142
Hao Zhufc14c9b2017-05-22 14:03:22 -0400143group_rows_latex <- function(kable_input, group_label, start_row, end_row,
georgegui4cc925b2018-03-01 12:02:45 -0600144 gap_space, escape, latex_align, colnum,
Brian Salzera3f43ad2018-03-04 15:03:20 -0500145 bold = T, italic = F, hline_before = F ,hline_after = F,
georgegui4cc925b2018-03-01 12:02:45 -0600146 extra_latex_after = NULL) {
Hao Zhud972e7f2017-05-22 13:27:15 -0400147 table_info <- magic_mirror(kable_input)
Hao Zhu3fc0e882018-04-03 16:06:41 -0400148 out <- solve_enc(kable_input)
Hao Zhud972e7f2017-05-22 13:27:15 -0400149
Hao Zhu064990d2017-10-17 18:08:42 -0400150 if (table_info$duplicated_rows) {
151 dup_fx_out <- fix_duplicated_rows_latex(out, table_info)
152 out <- dup_fx_out[[1]]
153 table_info <- dup_fx_out[[2]]
154 }
155
Hao Zhuac7e70f2017-08-02 00:18:36 -0400156 if (escape) {
Hao Zhuf94a26f2018-04-05 17:42:55 -0400157 group_label <- input_escape(group_label, latex_align)
Hao Zhuac7e70f2017-08-02 00:18:36 -0400158 }
159
Hao Zhuf94a26f2018-04-05 17:42:55 -0400160 if (bold) {
georgegui4cc925b2018-03-01 12:02:45 -0600161 group_label <- paste0("\\\\textbf{", group_label, "}")
162 }
Hao Zhuf94a26f2018-04-05 17:42:55 -0400163 if (italic) group_label <- paste0("\\\\textit{", group_label, "}")
Hao Zhud972e7f2017-05-22 13:27:15 -0400164 # Add group label
Hao Zhud972e7f2017-05-22 13:27:15 -0400165 if (table_info$booktabs) {
Hao Zhu37dbe3f2018-05-14 11:16:06 -0400166 rowtext <- table_info$contents[start_row + table_info$position_offset]
georgegui4cc925b2018-03-01 12:02:45 -0600167 pre_rowtext <- paste0(
Hao Zhud972e7f2017-05-22 13:27:15 -0400168 "\\\\addlinespace[", gap_space, "]\n",
Brian Salzera3f43ad2018-03-04 15:03:20 -0500169 ifelse(hline_before,"\\\\hline\n", ""),
bsalzer85933272018-02-12 17:26:46 -0500170 "\\\\multicolumn{", ifelse(is.null(colnum),
171 table_info$ncol,
172 colnum),
georgegui4cc925b2018-03-01 12:02:45 -0600173 "}{", latex_align, "}{", group_label,
174 "}\\\\\\\\\n", ifelse(hline_after, "\\\\hline\n", '')
Hao Zhud972e7f2017-05-22 13:27:15 -0400175 )
Hao Zhud972e7f2017-05-22 13:27:15 -0400176 } else {
Leo83f05132018-05-10 14:12:16 +0800177 rowtext <- table_info$contents[start_row + 1]
Hao Zhud972e7f2017-05-22 13:27:15 -0400178 rowtext <- paste0("\\\\hline\n", rowtext)
georgegui4cc925b2018-03-01 12:02:45 -0600179 pre_rowtext <- paste0(
180 "\\\\hline\n\\\\multicolumn{", table_info$ncol, "}{", latex_align,"}{",
181 group_label, "}\\\\\\\\\n"
Hao Zhud972e7f2017-05-22 13:27:15 -0400182 )
183 }
georgegui4cc925b2018-03-01 12:02:45 -0600184 if(!is.null(extra_latex_after)){
185 pre_rowtext <- paste0(pre_rowtext,
186 regex_escape(extra_latex_after, double_backslash = TRUE))
187 }
188 new_rowtext <- paste0(pre_rowtext, rowtext)
Hao Zhud972e7f2017-05-22 13:27:15 -0400189 out <- sub(rowtext, new_rowtext, out)
Hao Zhu8f202992017-07-15 02:20:18 -0400190 out <- gsub("\\\\addlinespace\n", "", out)
Hao Zhud2c0f732017-08-26 10:40:14 -0400191 out <- structure(out, format = "latex", class = "knitr_kable")
Hao Zhu32f43f72017-06-20 18:24:54 -0400192 table_info$group_rows_used <- TRUE
193 attr(out, "kable_meta") <- table_info
Hao Zhu49483bf2017-09-12 11:21:00 -0400194 out <- add_indent_latex(out, seq(start_row, end_row))
Hao Zhud972e7f2017-05-22 13:27:15 -0400195 return(out)
196}