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