Hao Zhu | f7994dd | 2017-02-27 16:58:42 -0500 | [diff] [blame] | 1 | #' 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 Zhu | 78e6122 | 2017-05-24 20:53:35 -0400 | [diff] [blame] | 15 | #' @examples x <- knitr::kable(head(mtcars), "html") |
| 16 | #' # Add a row of header with 3 columns on the top of the table. The column |
| 17 | #' # span for the 2nd and 3rd one are 5 & 6. |
| 18 | #' add_header_above(x, c(" ", "Group 1" = 5, "Group 2" = 6)) |
| 19 | #' |
Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 20 | #' @export |
| 21 | add_header_above <- function(kable_input, header = NULL) { |
| 22 | kable_format <- attr(kable_input, "format") |
| 23 | if (!kable_format %in% c("html", "latex")) { |
| 24 | stop("Please specify output format in your kable function. Currently ", |
| 25 | "generic markdown table using pandoc is not supported.") |
| 26 | } |
| 27 | if (kable_format == "html") { |
| 28 | return(htmlTable_add_header_above(kable_input, header)) |
| 29 | } |
| 30 | if (kable_format == "latex") { |
| 31 | return(pdfTable_add_header_above(kable_input, header)) |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | # HTML |
| 36 | htmlTable_add_header_above <- function(kable_input, header = NULL) { |
| 37 | if (is.null(header)) return(kable_input) |
Hao Zhu | 909ea38 | 2017-06-12 15:43:47 -0400 | [diff] [blame] | 38 | kable_attrs <- attributes(kable_input) |
Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 39 | kable_xml <- read_xml(as.character(kable_input), options = c("COMPACT")) |
Hao Zhu | 62cdde5 | 2017-05-20 22:16:03 -0400 | [diff] [blame] | 40 | kable_xml_thead <- xml_tpart(kable_xml, "thead") |
Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 41 | |
| 42 | header <- standardize_header_input(header) |
| 43 | |
| 44 | header_rows <- xml_children(kable_xml_thead) |
| 45 | bottom_header_row <- header_rows[[length(header_rows)]] |
| 46 | kable_ncol <- length(xml_children(bottom_header_row)) |
| 47 | if (sum(header$colspan) != kable_ncol) { |
| 48 | stop("The new header row you provided has a different total number of ", |
| 49 | "columns with the original kable output.") |
| 50 | } |
| 51 | |
| 52 | new_header_row <- htmlTable_new_header_generator(header) |
| 53 | xml_add_child(kable_xml_thead, new_header_row, .where = 0) |
Hao Zhu | 9b45a18 | 2017-02-27 18:17:46 -0500 | [diff] [blame] | 54 | out <- structure(as.character(kable_xml), format = "html", |
| 55 | class = "knitr_kable") |
Hao Zhu | 909ea38 | 2017-06-12 15:43:47 -0400 | [diff] [blame] | 56 | attributes(out) <- kable_attrs |
Hao Zhu | 6a07646 | 2017-03-01 12:59:01 -0500 | [diff] [blame] | 57 | return(out) |
Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | standardize_header_input <- function(header) { |
| 61 | header_names <- names(header) |
| 62 | |
| 63 | if (is.null(header_names)) { |
| 64 | return(data.frame(header = header, colspan = 1, row.names = NULL)) |
| 65 | } |
| 66 | |
| 67 | names(header)[header_names == ""] <- header[header_names == ""] |
| 68 | header[header_names == ""] <- 1 |
| 69 | header_names <- names(header) |
| 70 | header <- as.numeric(header) |
| 71 | names(header) <- header_names |
| 72 | return(data.frame(header = names(header), colspan = header, row.names = NULL)) |
| 73 | } |
| 74 | |
| 75 | htmlTable_new_header_generator <- function(header_df) { |
| 76 | header_items <- apply(header_df, 1, function(x) { |
Hao Zhu | acc7ceb | 2017-05-26 10:50:25 -0700 | [diff] [blame] | 77 | if (trimws(x[1]) == "") { |
Hao Zhu | 6d2faa1 | 2017-05-24 02:16:45 -0400 | [diff] [blame] | 78 | paste0('<th style="border-bottom:hidden"></th>') |
| 79 | } else { |
Hao Zhu | acc7ceb | 2017-05-26 10:50:25 -0700 | [diff] [blame] | 80 | paste0('<th style="text-align:center; border-bottom:hidden; ', |
| 81 | 'padding-bottom:0; padding-left:3px;padding-right:3px;" colspan="', |
Hao Zhu | 71e378a | 2017-05-26 11:00:21 -0700 | [diff] [blame] | 82 | x[2], '"><div style="border-bottom: 1px solid #ddd;padding-bottom: 5px;">', |
Hao Zhu | acc7ceb | 2017-05-26 10:50:25 -0700 | [diff] [blame] | 83 | x[1], '</div></th>') |
Hao Zhu | 6d2faa1 | 2017-05-24 02:16:45 -0400 | [diff] [blame] | 84 | } |
Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 85 | }) |
| 86 | header_text <- paste(c("<tr>", header_items, "</tr>"), collapse = "") |
| 87 | header_xml <- read_xml(header_text, options = c("COMPACT")) |
| 88 | return(header_xml) |
| 89 | } |
| 90 | |
| 91 | # Add an extra header row above the current header in a LaTeX table ------ |
| 92 | pdfTable_add_header_above <- function(kable_input, header = NULL) { |
| 93 | table_info <- magic_mirror(kable_input) |
| 94 | header <- standardize_header_input(header) |
Hao Zhu | 2ce42b9 | 2017-06-15 17:15:33 -0400 | [diff] [blame] | 95 | header$header <- escape_latex(header$header) |
| 96 | header$header <- gsub("\\\\", "\\\\\\\\", header$header) |
Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 97 | hline_type <- switch(table_info$booktabs + 1, "\\\\hline", "\\\\toprule") |
Hao Zhu | 2ce42b9 | 2017-06-15 17:15:33 -0400 | [diff] [blame] | 98 | new_header_split <- pdfTable_new_header_generator(header, table_info$booktabs) |
| 99 | new_header <- paste0(new_header_split[1], "\n", new_header_split[2]) |
Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 100 | out <- sub(hline_type, |
Hao Zhu | 2ce42b9 | 2017-06-15 17:15:33 -0400 | [diff] [blame] | 101 | paste0(hline_type, "\n", new_header), |
Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 102 | as.character(kable_input)) |
| 103 | out <- structure(out, format = "latex", class = "knitr_kable") |
Hao Zhu | 2ce42b9 | 2017-06-15 17:15:33 -0400 | [diff] [blame] | 104 | # new_header_row <- latex_contents_escape(new_header_split[1]) |
| 105 | if (is.null(table_info$new_header_row)) { |
| 106 | table_info$new_header_row <- new_header_split[1] |
| 107 | table_info$header_df <- list(header) |
| 108 | } else { |
| 109 | table_info$new_header_row <- c(table_info$new_header_row, new_header_split[1]) |
| 110 | table_info$header_df[[length(table_info$header_df) + 1]] <- header |
| 111 | } |
Hao Zhu | 9b45a18 | 2017-02-27 18:17:46 -0500 | [diff] [blame] | 112 | attr(out, "original_kable_meta") <- table_info |
Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 113 | return(out) |
| 114 | } |
| 115 | |
| 116 | pdfTable_new_header_generator <- function(header_df, booktabs = F) { |
| 117 | if (booktabs) { |
| 118 | header_df$align <- "c" |
| 119 | } else { |
| 120 | header_df$align <- "|c|" |
| 121 | header_df$align[1] <- "c|" |
| 122 | header_df$align[nrow(header_df)] <- "|c" |
| 123 | } |
| 124 | header_items <- apply(header_df, 1, function(x) { |
| 125 | # if(x[2] == 1) return(x[1]) |
| 126 | paste0('\\\\multicolumn{', x[2], '}{', x[3], '}{', x[1], "}") |
| 127 | }) |
| 128 | header_text <- paste(paste(header_items, collapse = " & "), "\\\\\\\\") |
Hao Zhu | 2ce42b9 | 2017-06-15 17:15:33 -0400 | [diff] [blame] | 129 | cline <- cline_gen(header_df, booktabs) |
| 130 | return(c(header_text, cline)) |
| 131 | } |
| 132 | |
| 133 | cline_gen <- function(header_df, booktabs) { |
Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 134 | cline_end <- cumsum(header_df$colspan) |
| 135 | cline_start <- c(0, cline_end) + 1 |
| 136 | cline_start <- cline_start[-length(cline_start)] |
Hao Zhu | c05e181 | 2017-02-25 01:45:35 -0500 | [diff] [blame] | 137 | cline_type <- switch(booktabs + 1, |
| 138 | "\\\\cline{", |
| 139 | "\\\\cmidrule(l{2pt}r{2pt}){") |
Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 140 | cline <- paste0(cline_type, cline_start, "-", cline_end, "}") |
| 141 | cline <- cline[trimws(header_df$header) != ""] |
| 142 | cline <- paste(cline, collapse = " ") |
Hao Zhu | 2ce42b9 | 2017-06-15 17:15:33 -0400 | [diff] [blame] | 143 | return(cline) |
Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 144 | } |
Hao Zhu | 2ce42b9 | 2017-06-15 17:15:33 -0400 | [diff] [blame] | 145 | |
| 146 | |