blob: 1242868ed498c4254673e421784c50fbe2d72452 [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 Zhuc1f38412017-02-23 12:13:48 -050041 kable_xml <- read_xml(as.character(kable_input), options = c("COMPACT"))
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 Zhu9b45a182017-02-27 18:17:46 -050056 out <- structure(as.character(kable_xml), format = "html",
57 class = "knitr_kable")
Hao Zhu909ea382017-06-12 15:43:47 -040058 attributes(out) <- kable_attrs
Hao Zhu6a076462017-03-01 12:59:01 -050059 return(out)
Hao Zhuc1f38412017-02-23 12:13:48 -050060}
61
62standardize_header_input <- function(header) {
63 header_names <- names(header)
64
65 if (is.null(header_names)) {
66 return(data.frame(header = header, colspan = 1, row.names = NULL))
67 }
68
69 names(header)[header_names == ""] <- header[header_names == ""]
70 header[header_names == ""] <- 1
71 header_names <- names(header)
72 header <- as.numeric(header)
73 names(header) <- header_names
74 return(data.frame(header = names(header), colspan = header, row.names = NULL))
75}
76
Hao Zhu32f43f72017-06-20 18:24:54 -040077htmlTable_new_header_generator <- function(header_df, bold, italic) {
78 row_style <- paste0(
79 ifelse(bold, "font-weight: bold; ", ""),
80 ifelse(italic, "font-style: italic; ", "")
81 )
Hao Zhuc1f38412017-02-23 12:13:48 -050082 header_items <- apply(header_df, 1, function(x) {
Hao Zhuacc7ceb2017-05-26 10:50:25 -070083 if (trimws(x[1]) == "") {
Hao Zhu6d2faa12017-05-24 02:16:45 -040084 paste0('<th style="border-bottom:hidden"></th>')
85 } else {
Hao Zhuacc7ceb2017-05-26 10:50:25 -070086 paste0('<th style="text-align:center; border-bottom:hidden; ',
Hao Zhu32f43f72017-06-20 18:24:54 -040087 'padding-bottom:0; padding-left:3px;padding-right:3px;',
88 row_style,
89 '" colspan="',
Hao Zhu71e378a2017-05-26 11:00:21 -070090 x[2], '"><div style="border-bottom: 1px solid #ddd;padding-bottom: 5px;">',
Hao Zhuacc7ceb2017-05-26 10:50:25 -070091 x[1], '</div></th>')
Hao Zhu6d2faa12017-05-24 02:16:45 -040092 }
Hao Zhuc1f38412017-02-23 12:13:48 -050093 })
94 header_text <- paste(c("<tr>", header_items, "</tr>"), collapse = "")
95 header_xml <- read_xml(header_text, options = c("COMPACT"))
96 return(header_xml)
97}
98
99# Add an extra header row above the current header in a LaTeX table ------
Hao Zhu32f43f72017-06-20 18:24:54 -0400100pdfTable_add_header_above <- function(kable_input, header, bold, italic) {
Hao Zhuc1f38412017-02-23 12:13:48 -0500101 table_info <- magic_mirror(kable_input)
102 header <- standardize_header_input(header)
Hao Zhu2ce42b92017-06-15 17:15:33 -0400103 header$header <- escape_latex(header$header)
104 header$header <- gsub("\\\\", "\\\\\\\\", header$header)
Hao Zhuc1f38412017-02-23 12:13:48 -0500105 hline_type <- switch(table_info$booktabs + 1, "\\\\hline", "\\\\toprule")
Hao Zhu32f43f72017-06-20 18:24:54 -0400106 new_header_split <- pdfTable_new_header_generator(header, table_info$booktabs,
107 bold, italic)
Hao Zhu2ce42b92017-06-15 17:15:33 -0400108 new_header <- paste0(new_header_split[1], "\n", new_header_split[2])
Hao Zhuc1f38412017-02-23 12:13:48 -0500109 out <- sub(hline_type,
Hao Zhu2ce42b92017-06-15 17:15:33 -0400110 paste0(hline_type, "\n", new_header),
Hao Zhuc1f38412017-02-23 12:13:48 -0500111 as.character(kable_input))
112 out <- structure(out, format = "latex", class = "knitr_kable")
Hao Zhu2ce42b92017-06-15 17:15:33 -0400113 # new_header_row <- latex_contents_escape(new_header_split[1])
114 if (is.null(table_info$new_header_row)) {
115 table_info$new_header_row <- new_header_split[1]
116 table_info$header_df <- list(header)
117 } else {
118 table_info$new_header_row <- c(table_info$new_header_row, new_header_split[1])
119 table_info$header_df[[length(table_info$header_df) + 1]] <- header
120 }
Hao Zhu32f43f72017-06-20 18:24:54 -0400121 attr(out, "kable_meta") <- table_info
Hao Zhuc1f38412017-02-23 12:13:48 -0500122 return(out)
123}
124
Hao Zhu32f43f72017-06-20 18:24:54 -0400125pdfTable_new_header_generator <- function(header_df, booktabs = FALSE,
126 bold, italic) {
Hao Zhuc1f38412017-02-23 12:13:48 -0500127 if (booktabs) {
128 header_df$align <- "c"
129 } else {
130 header_df$align <- "|c|"
131 header_df$align[1] <- "c|"
132 header_df$align[nrow(header_df)] <- "|c"
133 }
134 header_items <- apply(header_df, 1, function(x) {
135 # if(x[2] == 1) return(x[1])
Hao Zhu32f43f72017-06-20 18:24:54 -0400136 paste0('\\\\multicolumn{', x[2], '}{', x[3], '}{',
137 ifelse(bold, "\\\\bfseries ", ""),
138 ifelse(italic, "\\\\em ", ""),
139 x[1], "}")
Hao Zhuc1f38412017-02-23 12:13:48 -0500140 })
141 header_text <- paste(paste(header_items, collapse = " & "), "\\\\\\\\")
Hao Zhu2ce42b92017-06-15 17:15:33 -0400142 cline <- cline_gen(header_df, booktabs)
143 return(c(header_text, cline))
144}
145
146cline_gen <- function(header_df, booktabs) {
Hao Zhuc1f38412017-02-23 12:13:48 -0500147 cline_end <- cumsum(header_df$colspan)
148 cline_start <- c(0, cline_end) + 1
149 cline_start <- cline_start[-length(cline_start)]
Hao Zhuc05e1812017-02-25 01:45:35 -0500150 cline_type <- switch(booktabs + 1,
151 "\\\\cline{",
152 "\\\\cmidrule(l{2pt}r{2pt}){")
Hao Zhuc1f38412017-02-23 12:13:48 -0500153 cline <- paste0(cline_type, cline_start, "-", cline_end, "}")
154 cline <- cline[trimws(header_df$header) != ""]
155 cline <- paste(cline, collapse = " ")
Hao Zhu2ce42b92017-06-15 17:15:33 -0400156 return(cline)
Hao Zhuc1f38412017-02-23 12:13:48 -0500157}
Hao Zhu2ce42b92017-06-15 17:15:33 -0400158
159