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 | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 15 | #' @export |
| 16 | add_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 |
| 31 | htmlTable_add_header_above <- function(kable_input, header = NULL) { |
| 32 | if (is.null(header)) return(kable_input) |
| 33 | kable_xml <- read_xml(as.character(kable_input), options = c("COMPACT")) |
| 34 | # somehow xml2 cannot directly search by name here (it will result in a crash) |
| 35 | kable_xml_thead <- xml_child(kable_xml, 1) |
| 36 | if (xml_name(kable_xml_thead) != "thead") { |
| 37 | kable_xml_thead <- xml_child(kable_xml, 2) |
| 38 | } |
| 39 | |
| 40 | header <- standardize_header_input(header) |
| 41 | |
| 42 | header_rows <- xml_children(kable_xml_thead) |
| 43 | bottom_header_row <- header_rows[[length(header_rows)]] |
| 44 | kable_ncol <- length(xml_children(bottom_header_row)) |
| 45 | if (sum(header$colspan) != kable_ncol) { |
| 46 | stop("The new header row you provided has a different total number of ", |
| 47 | "columns with the original kable output.") |
| 48 | } |
| 49 | |
| 50 | new_header_row <- htmlTable_new_header_generator(header) |
| 51 | xml_add_child(kable_xml_thead, new_header_row, .where = 0) |
| 52 | return(structure(as.character(kable_xml), format = "html", |
| 53 | class = "knitr_kable")) |
| 54 | } |
| 55 | |
| 56 | standardize_header_input <- function(header) { |
| 57 | header_names <- names(header) |
| 58 | |
| 59 | if (is.null(header_names)) { |
| 60 | return(data.frame(header = header, colspan = 1, row.names = NULL)) |
| 61 | } |
| 62 | |
| 63 | names(header)[header_names == ""] <- header[header_names == ""] |
| 64 | header[header_names == ""] <- 1 |
| 65 | header_names <- names(header) |
| 66 | header <- as.numeric(header) |
| 67 | names(header) <- header_names |
| 68 | return(data.frame(header = names(header), colspan = header, row.names = NULL)) |
| 69 | } |
| 70 | |
| 71 | htmlTable_new_header_generator <- function(header_df) { |
| 72 | header_items <- apply(header_df, 1, function(x) { |
| 73 | paste0('<th style="text-align:center;" colspan="', x[2], '">', |
| 74 | x[1], '</th>') |
| 75 | }) |
| 76 | header_text <- paste(c("<tr>", header_items, "</tr>"), collapse = "") |
| 77 | header_xml <- read_xml(header_text, options = c("COMPACT")) |
| 78 | return(header_xml) |
| 79 | } |
| 80 | |
| 81 | # Add an extra header row above the current header in a LaTeX table ------ |
| 82 | pdfTable_add_header_above <- function(kable_input, header = NULL) { |
| 83 | table_info <- magic_mirror(kable_input) |
| 84 | header <- standardize_header_input(header) |
| 85 | hline_type <- switch(table_info$booktabs + 1, "\\\\hline", "\\\\toprule") |
| 86 | out <- sub(hline_type, |
| 87 | paste0(hline_type, "\n", |
| 88 | pdfTable_new_header_generator(header, table_info$booktabs)), |
| 89 | as.character(kable_input)) |
| 90 | out <- structure(out, format = "latex", class = "knitr_kable") |
| 91 | return(out) |
| 92 | } |
| 93 | |
| 94 | pdfTable_new_header_generator <- function(header_df, booktabs = F) { |
| 95 | if (booktabs) { |
| 96 | header_df$align <- "c" |
| 97 | } else { |
| 98 | header_df$align <- "|c|" |
| 99 | header_df$align[1] <- "c|" |
| 100 | header_df$align[nrow(header_df)] <- "|c" |
| 101 | } |
| 102 | header_items <- apply(header_df, 1, function(x) { |
| 103 | # if(x[2] == 1) return(x[1]) |
| 104 | paste0('\\\\multicolumn{', x[2], '}{', x[3], '}{', x[1], "}") |
| 105 | }) |
| 106 | header_text <- paste(paste(header_items, collapse = " & "), "\\\\\\\\") |
| 107 | cline_end <- cumsum(header_df$colspan) |
| 108 | cline_start <- c(0, cline_end) + 1 |
| 109 | cline_start <- cline_start[-length(cline_start)] |
Hao Zhu | c05e181 | 2017-02-25 01:45:35 -0500 | [diff] [blame] | 110 | cline_type <- switch(booktabs + 1, |
| 111 | "\\\\cline{", |
| 112 | "\\\\cmidrule(l{2pt}r{2pt}){") |
Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 113 | cline <- paste0(cline_type, cline_start, "-", cline_end, "}") |
| 114 | cline <- cline[trimws(header_df$header) != ""] |
| 115 | cline <- paste(cline, collapse = " ") |
| 116 | header_text <- paste(header_text, cline) |
| 117 | return(header_text) |
| 118 | } |