blob: 92e2040ca5d2e72c454bf618195102b10508666d [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)`.
14#'
Hao Zhuc1f38412017-02-23 12:13:48 -050015#' @export
16add_header_above <- function(kable_input, header = NULL) {
17 kable_format <- attr(kable_input, "format")
18 if (!kable_format %in% c("html", "latex")) {
19 stop("Please specify output format in your kable function. Currently ",
20 "generic markdown table using pandoc is not supported.")
21 }
22 if (kable_format == "html") {
23 return(htmlTable_add_header_above(kable_input, header))
24 }
25 if (kable_format == "latex") {
26 return(pdfTable_add_header_above(kable_input, header))
27 }
28}
29
30# HTML
31htmlTable_add_header_above <- function(kable_input, header = NULL) {
32 if (is.null(header)) return(kable_input)
Hao Zhu9b45a182017-02-27 18:17:46 -050033 table_info <- magic_mirror(kable_input)
Hao Zhuc1f38412017-02-23 12:13:48 -050034 kable_xml <- read_xml(as.character(kable_input), options = c("COMPACT"))
Hao Zhu62cdde52017-05-20 22:16:03 -040035 kable_xml_thead <- xml_tpart(kable_xml, "thead")
Hao Zhuc1f38412017-02-23 12:13:48 -050036
37 header <- standardize_header_input(header)
38
39 header_rows <- xml_children(kable_xml_thead)
40 bottom_header_row <- header_rows[[length(header_rows)]]
41 kable_ncol <- length(xml_children(bottom_header_row))
42 if (sum(header$colspan) != kable_ncol) {
43 stop("The new header row you provided has a different total number of ",
44 "columns with the original kable output.")
45 }
46
47 new_header_row <- htmlTable_new_header_generator(header)
48 xml_add_child(kable_xml_thead, new_header_row, .where = 0)
Hao Zhu9b45a182017-02-27 18:17:46 -050049 out <- structure(as.character(kable_xml), format = "html",
50 class = "knitr_kable")
51 attr(out, "original_kable_meta") <- table_info
Hao Zhu6a076462017-03-01 12:59:01 -050052 return(out)
Hao Zhuc1f38412017-02-23 12:13:48 -050053}
54
55standardize_header_input <- function(header) {
56 header_names <- names(header)
57
58 if (is.null(header_names)) {
59 return(data.frame(header = header, colspan = 1, row.names = NULL))
60 }
61
62 names(header)[header_names == ""] <- header[header_names == ""]
63 header[header_names == ""] <- 1
64 header_names <- names(header)
65 header <- as.numeric(header)
66 names(header) <- header_names
67 return(data.frame(header = names(header), colspan = header, row.names = NULL))
68}
69
70htmlTable_new_header_generator <- function(header_df) {
71 header_items <- apply(header_df, 1, function(x) {
Hao Zhu6d2faa12017-05-24 02:16:45 -040072 if (x[2] == 1 | trimws(x[1]) == "") {
73 paste0('<th style="border-bottom:hidden"></th>')
74 } else {
75 paste0('<th style="text-align:center;" colspan="', x[2], '">',
76 x[1], '</th>')
77 }
Hao Zhuc1f38412017-02-23 12:13:48 -050078 })
79 header_text <- paste(c("<tr>", header_items, "</tr>"), collapse = "")
80 header_xml <- read_xml(header_text, options = c("COMPACT"))
81 return(header_xml)
82}
83
84# Add an extra header row above the current header in a LaTeX table ------
85pdfTable_add_header_above <- function(kable_input, header = NULL) {
86 table_info <- magic_mirror(kable_input)
87 header <- standardize_header_input(header)
88 hline_type <- switch(table_info$booktabs + 1, "\\\\hline", "\\\\toprule")
89 out <- sub(hline_type,
90 paste0(hline_type, "\n",
91 pdfTable_new_header_generator(header, table_info$booktabs)),
92 as.character(kable_input))
93 out <- structure(out, format = "latex", class = "knitr_kable")
Hao Zhu9b45a182017-02-27 18:17:46 -050094 attr(out, "original_kable_meta") <- table_info
Hao Zhuc1f38412017-02-23 12:13:48 -050095 return(out)
96}
97
98pdfTable_new_header_generator <- function(header_df, booktabs = F) {
99 if (booktabs) {
100 header_df$align <- "c"
101 } else {
102 header_df$align <- "|c|"
103 header_df$align[1] <- "c|"
104 header_df$align[nrow(header_df)] <- "|c"
105 }
106 header_items <- apply(header_df, 1, function(x) {
107 # if(x[2] == 1) return(x[1])
108 paste0('\\\\multicolumn{', x[2], '}{', x[3], '}{', x[1], "}")
109 })
110 header_text <- paste(paste(header_items, collapse = " & "), "\\\\\\\\")
111 cline_end <- cumsum(header_df$colspan)
112 cline_start <- c(0, cline_end) + 1
113 cline_start <- cline_start[-length(cline_start)]
Hao Zhuc05e1812017-02-25 01:45:35 -0500114 cline_type <- switch(booktabs + 1,
115 "\\\\cline{",
116 "\\\\cmidrule(l{2pt}r{2pt}){")
Hao Zhuc1f38412017-02-23 12:13:48 -0500117 cline <- paste0(cline_type, cline_start, "-", cline_end, "}")
118 cline <- cline[trimws(header_df$header) != ""]
119 cline <- paste(cline, collapse = " ")
120 header_text <- paste(header_text, cline)
121 return(header_text)
122}