blob: c38aaaeeb81fa10207121a572038e6021a59a7e7 [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.
12#' @param label_row_css A character string for any customized css used for the
13#' labeling row. By default, the labeling row will have a solid black line
14#' underneath. Only useful for HTML documents.
15#' @param latex_gap_space A character value telling LaTeX how large the gap
16#' between the previous row and the group labeling row. Only useful for LaTeX
17#' documents.
18#'
Hao Zhu78e61222017-05-24 20:53:35 -040019#' @examples x <- knitr::kable(head(mtcars), "html")
20#' # Put Row 2 to Row 5 into a Group and label it as "Group A"
21#' group_rows(x, "Group A", 2, 5)
22#'
Hao Zhu62cdde52017-05-20 22:16:03 -040023#' @export
Hao Zhud972e7f2017-05-22 13:27:15 -040024group_rows <- function(kable_input, group_label, start_row, end_row,
25 label_row_css = "border-bottom: 1px solid;",
26 latex_gap_space = "0.5em") {
27 if (!is.numeric(c(start_row, end_row))) {
28 stop("Start_row and end_row must be numeric position of rows (excluding",
29 "header rows and other group-title rows). ")
30 }
Hao Zhu62cdde52017-05-20 22:16:03 -040031 kable_format <- attr(kable_input, "format")
32 if (!kable_format %in% c("html", "latex")) {
33 message("Currently generic markdown table using pandoc is not supported.")
34 return(kable_input)
35 }
36 if (kable_format == "html") {
Hao Zhud972e7f2017-05-22 13:27:15 -040037 return(group_rows_html(kable_input, group_label, start_row, end_row,
38 label_row_css))
Hao Zhu62cdde52017-05-20 22:16:03 -040039 }
40 if (kable_format == "latex") {
Hao Zhud972e7f2017-05-22 13:27:15 -040041 return(group_rows_latex(kable_input, group_label, start_row, end_row,
42 latex_gap_space))
Hao Zhu62cdde52017-05-20 22:16:03 -040043 }
44}
45
Hao Zhud972e7f2017-05-22 13:27:15 -040046group_rows_html <- function(kable_input, group_label, start_row, end_row,
47 label_row_css) {
Hao Zhu62cdde52017-05-20 22:16:03 -040048 kable_attrs <- attributes(kable_input)
49 kable_xml <- read_xml(as.character(kable_input), options = "COMPACT")
50 kable_tbody <- xml_tpart(kable_xml, "tbody")
51
52 group_header_rows <- attr(kable_input, "group_header_rows")
53 group_seq <- seq(start_row, end_row)
54 if (!is.null(group_header_rows)) {
55 group_seq <- positions_corrector(group_seq, group_header_rows,
56 length(xml_children(kable_tbody)))
57 }
58
59 # Insert a group header row
60 starting_node <- xml_child(kable_tbody, group_seq[1])
61 kable_ncol <- length(xml_children(starting_node))
62 group_header_row_text <- paste0(
Hao Zhud972e7f2017-05-22 13:27:15 -040063 '<tr groupLength="', length(group_seq), '"><td colspan="', kable_ncol,
64 '" style="', label_row_css, '"><strong>', group_label,
65 "</strong></td></tr>"
Hao Zhu62cdde52017-05-20 22:16:03 -040066 )
67 group_header_row <- read_xml(group_header_row_text, options = "COMPACT")
68 xml_add_sibling(starting_node, group_header_row, .where = "before")
69
70 # add indentations to items
71 out <- structure(as.character(kable_xml), format = "html",
72 class = "knitr_kable")
73 attributes(out) <- kable_attrs
74 attr(out, "group_header_rows") <- c(attr(out, "group_header_rows"), group_seq[1])
75 out <- add_indent(out, positions = seq(start_row, end_row))
76 return(out)
77}
Hao Zhud972e7f2017-05-22 13:27:15 -040078
Hao Zhufc14c9b2017-05-22 14:03:22 -040079group_rows_latex <- function(kable_input, group_label, start_row, end_row,
80 gap_space) {
Hao Zhud972e7f2017-05-22 13:27:15 -040081 table_info <- magic_mirror(kable_input)
82 out <- kable_input
83
84 # Add group label
85 rowtext <- table_info$contents[start_row + 1]
86 if (table_info$booktabs) {
87 new_rowtext <- paste0(
88 "\\\\addlinespace[", gap_space, "]\n",
89 "\\\\multicolumn{", table_info$ncol, "}{l}{\\\\textbf{", group_label,
90 "}}\\\\\\\\\n",
91 rowtext
92 )
Hao Zhud972e7f2017-05-22 13:27:15 -040093 } else {
94 rowtext <- paste0("\\\\hline\n", rowtext)
95 new_rowtext <- paste0(
96 "\\\\hline\n\\\\multicolumn{", table_info$ncol, "}{l}{\\\\textbf{",
97 group_label, "}}\\\\\\\\\n", rowtext
98 )
99 }
100 out <- sub(rowtext, new_rowtext, out)
Hao Zhu8f202992017-07-15 02:20:18 -0400101 out <- gsub("\\\\addlinespace\n", "", out)
Hao Zhu32f43f72017-06-20 18:24:54 -0400102 table_info$group_rows_used <- TRUE
103 attr(out, "kable_meta") <- table_info
Hao Zhud972e7f2017-05-22 13:27:15 -0400104 out <- add_indent(out, seq(start_row, end_row))
105 return(out)
106}