blob: 5716d84dc3f26c816b3a19f0c584d7a85c2f89f8 [file] [log] [blame]
Hao Zhuf7994dd2017-02-27 16:58:42 -05001#' Add a header row on top of current header
2#'
3#' @description Tables with multiple rows of header rows are extremely useful
4#' to demonstrate grouped data. This function takes the output of a `kable()`
5#' function and adds an header row on top of it. This function can work with
6#' both `HTML` and `LaTeX` outputs
7#'
8#' @param kable_input Output of `knitr::kable()` with `format` specified
9#' @param header A (named) character vector with `colspan` as values. For
10#' example, `c(" " = 1, "title" = 2)` can be used to create a new header row
11#' for a 3-column table with "title" spanning across column 2 and 3. For
12#' convenience, when `colspan` equals to 1, users can drop the ` = 1` part.
13#' As a result, `c(" ", "title" = 2)` is the same as `c(" " = 1, "title" = 2)`.
Hao Zhu32f43f72017-06-20 18:24:54 -040014#' @param bold A T/F value to control whether the text should be bolded.
15#' @param italic A T/F value to control whether the text should to be emphasized.
Hao Zhuf7994dd2017-02-27 16:58:42 -050016#'
Hao Zhu78e61222017-05-24 20:53:35 -040017#' @examples x <- knitr::kable(head(mtcars), "html")
18#' # Add a row of header with 3 columns on the top of the table. The column
19#' # span for the 2nd and 3rd one are 5 & 6.
20#' add_header_above(x, c(" ", "Group 1" = 5, "Group 2" = 6))
21#'
Hao Zhuc1f38412017-02-23 12:13:48 -050022#' @export
Hao Zhu32f43f72017-06-20 18:24:54 -040023add_header_above <- function(kable_input, header = NULL, bold = F, italic = F) {
Hao Zhuc1f38412017-02-23 12:13:48 -050024 kable_format <- attr(kable_input, "format")
25 if (!kable_format %in% c("html", "latex")) {
26 stop("Please specify output format in your kable function. Currently ",
27 "generic markdown table using pandoc is not supported.")
28 }
29 if (kable_format == "html") {
Hao Zhu32f43f72017-06-20 18:24:54 -040030 return(htmlTable_add_header_above(kable_input, header, bold, italic))
Hao Zhuc1f38412017-02-23 12:13:48 -050031 }
32 if (kable_format == "latex") {
Hao Zhu32f43f72017-06-20 18:24:54 -040033 return(pdfTable_add_header_above(kable_input, header, bold, italic))
Hao Zhuc1f38412017-02-23 12:13:48 -050034 }
35}
36
37# HTML
Hao Zhu32f43f72017-06-20 18:24:54 -040038htmlTable_add_header_above <- function(kable_input, header, bold, italic) {
Hao Zhuc1f38412017-02-23 12:13:48 -050039 if (is.null(header)) return(kable_input)
Hao Zhu909ea382017-06-12 15:43:47 -040040 kable_attrs <- attributes(kable_input)
Hao Zhu558c72f2017-07-24 15:12:00 -040041 kable_xml <- read_kable_as_xml(kable_input)
Hao Zhu62cdde52017-05-20 22:16:03 -040042 kable_xml_thead <- xml_tpart(kable_xml, "thead")
Hao Zhuc1f38412017-02-23 12:13:48 -050043
44 header <- standardize_header_input(header)
45
46 header_rows <- xml_children(kable_xml_thead)
47 bottom_header_row <- header_rows[[length(header_rows)]]
48 kable_ncol <- length(xml_children(bottom_header_row))
49 if (sum(header$colspan) != kable_ncol) {
50 stop("The new header row you provided has a different total number of ",
51 "columns with the original kable output.")
52 }
53
Hao Zhu32f43f72017-06-20 18:24:54 -040054 new_header_row <- htmlTable_new_header_generator(header, bold, italic)
Hao Zhuc1f38412017-02-23 12:13:48 -050055 xml_add_child(kable_xml_thead, new_header_row, .where = 0)
Hao Zhuf2dfd142017-07-24 14:43:28 -040056 out <- as_kable_xml(kable_xml)
Hao Zhu909ea382017-06-12 15:43:47 -040057 attributes(out) <- kable_attrs
Hao Zhu6a076462017-03-01 12:59:01 -050058 return(out)
Hao Zhuc1f38412017-02-23 12:13:48 -050059}
60
61standardize_header_input <- function(header) {
62 header_names <- names(header)
63
64 if (is.null(header_names)) {
65 return(data.frame(header = header, colspan = 1, row.names = NULL))
66 }
67
68 names(header)[header_names == ""] <- header[header_names == ""]
69 header[header_names == ""] <- 1
70 header_names <- names(header)
71 header <- as.numeric(header)
72 names(header) <- header_names
73 return(data.frame(header = names(header), colspan = header, row.names = NULL))
74}
75
Hao Zhu32f43f72017-06-20 18:24:54 -040076htmlTable_new_header_generator <- function(header_df, bold, italic) {
77 row_style <- paste0(
78 ifelse(bold, "font-weight: bold; ", ""),
79 ifelse(italic, "font-style: italic; ", "")
80 )
Hao Zhuc1f38412017-02-23 12:13:48 -050081 header_items <- apply(header_df, 1, function(x) {
Hao Zhuacc7ceb2017-05-26 10:50:25 -070082 if (trimws(x[1]) == "") {
Hao Zhu6d2faa12017-05-24 02:16:45 -040083 paste0('<th style="border-bottom:hidden"></th>')
84 } else {
Hao Zhuacc7ceb2017-05-26 10:50:25 -070085 paste0('<th style="text-align:center; border-bottom:hidden; ',
Hao Zhu32f43f72017-06-20 18:24:54 -040086 'padding-bottom:0; padding-left:3px;padding-right:3px;',
87 row_style,
88 '" colspan="',
Hao Zhu4b0c51e2017-08-01 15:21:07 -040089 x[2], '"><div style="border-bottom: 1px solid #ddd; padding-bottom: 5px;">',
Hao Zhuacc7ceb2017-05-26 10:50:25 -070090 x[1], '</div></th>')
Hao Zhu6d2faa12017-05-24 02:16:45 -040091 }
Hao Zhuc1f38412017-02-23 12:13:48 -050092 })
93 header_text <- paste(c("<tr>", header_items, "</tr>"), collapse = "")
94 header_xml <- read_xml(header_text, options = c("COMPACT"))
95 return(header_xml)
96}
97
98# Add an extra header row above the current header in a LaTeX table ------
Hao Zhu32f43f72017-06-20 18:24:54 -040099pdfTable_add_header_above <- function(kable_input, header, bold, italic) {
Hao Zhuc1f38412017-02-23 12:13:48 -0500100 table_info <- magic_mirror(kable_input)
101 header <- standardize_header_input(header)
Hao Zhu2ce42b92017-06-15 17:15:33 -0400102 header$header <- escape_latex(header$header)
103 header$header <- gsub("\\\\", "\\\\\\\\", header$header)
Hao Zhuc1f38412017-02-23 12:13:48 -0500104 hline_type <- switch(table_info$booktabs + 1, "\\\\hline", "\\\\toprule")
Hao Zhu32f43f72017-06-20 18:24:54 -0400105 new_header_split <- pdfTable_new_header_generator(header, table_info$booktabs,
106 bold, italic)
Hao Zhu2ce42b92017-06-15 17:15:33 -0400107 new_header <- paste0(new_header_split[1], "\n", new_header_split[2])
Hao Zhuc1f38412017-02-23 12:13:48 -0500108 out <- sub(hline_type,
Hao Zhu2ce42b92017-06-15 17:15:33 -0400109 paste0(hline_type, "\n", new_header),
Hao Zhuc1f38412017-02-23 12:13:48 -0500110 as.character(kable_input))
111 out <- structure(out, format = "latex", class = "knitr_kable")
Hao Zhu2ce42b92017-06-15 17:15:33 -0400112 # new_header_row <- latex_contents_escape(new_header_split[1])
113 if (is.null(table_info$new_header_row)) {
114 table_info$new_header_row <- new_header_split[1]
115 table_info$header_df <- list(header)
116 } else {
117 table_info$new_header_row <- c(table_info$new_header_row, new_header_split[1])
118 table_info$header_df[[length(table_info$header_df) + 1]] <- header
119 }
Hao Zhu32f43f72017-06-20 18:24:54 -0400120 attr(out, "kable_meta") <- table_info
Hao Zhuc1f38412017-02-23 12:13:48 -0500121 return(out)
122}
123
Hao Zhu32f43f72017-06-20 18:24:54 -0400124pdfTable_new_header_generator <- function(header_df, booktabs = FALSE,
125 bold, italic) {
Hao Zhuc1f38412017-02-23 12:13:48 -0500126 if (booktabs) {
127 header_df$align <- "c"
128 } else {
129 header_df$align <- "|c|"
130 header_df$align[1] <- "c|"
131 header_df$align[nrow(header_df)] <- "|c"
132 }
133 header_items <- apply(header_df, 1, function(x) {
134 # if(x[2] == 1) return(x[1])
Hao Zhu32f43f72017-06-20 18:24:54 -0400135 paste0('\\\\multicolumn{', x[2], '}{', x[3], '}{',
136 ifelse(bold, "\\\\bfseries ", ""),
137 ifelse(italic, "\\\\em ", ""),
138 x[1], "}")
Hao Zhuc1f38412017-02-23 12:13:48 -0500139 })
140 header_text <- paste(paste(header_items, collapse = " & "), "\\\\\\\\")
Hao Zhu2ce42b92017-06-15 17:15:33 -0400141 cline <- cline_gen(header_df, booktabs)
142 return(c(header_text, cline))
143}
144
145cline_gen <- function(header_df, booktabs) {
Hao Zhuc1f38412017-02-23 12:13:48 -0500146 cline_end <- cumsum(header_df$colspan)
147 cline_start <- c(0, cline_end) + 1
148 cline_start <- cline_start[-length(cline_start)]
Hao Zhuc05e1812017-02-25 01:45:35 -0500149 cline_type <- switch(booktabs + 1,
150 "\\\\cline{",
151 "\\\\cmidrule(l{2pt}r{2pt}){")
Hao Zhuc1f38412017-02-23 12:13:48 -0500152 cline <- paste0(cline_type, cline_start, "-", cline_end, "}")
153 cline <- cline[trimws(header_df$header) != ""]
154 cline <- paste(cline, collapse = " ")
Hao Zhu2ce42b92017-06-15 17:15:33 -0400155 return(cline)
Hao Zhuc1f38412017-02-23 12:13:48 -0500156}
Hao Zhu2ce42b92017-06-15 17:15:33 -0400157
158