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)`. |
Hao Zhu | 32f43f7 | 2017-06-20 18:24:54 -0400 | [diff] [blame] | 14 | #' @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 Zhu | f7994dd | 2017-02-27 16:58:42 -0500 | [diff] [blame] | 16 | #' |
Hao Zhu | 78e6122 | 2017-05-24 20:53:35 -0400 | [diff] [blame] | 17 | #' @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 Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 22 | #' @export |
Hao Zhu | 32f43f7 | 2017-06-20 18:24:54 -0400 | [diff] [blame] | 23 | add_header_above <- function(kable_input, header = NULL, bold = F, italic = F) { |
Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 24 | 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 Zhu | 32f43f7 | 2017-06-20 18:24:54 -0400 | [diff] [blame] | 30 | return(htmlTable_add_header_above(kable_input, header, bold, italic)) |
Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 31 | } |
| 32 | if (kable_format == "latex") { |
Hao Zhu | 32f43f7 | 2017-06-20 18:24:54 -0400 | [diff] [blame] | 33 | return(pdfTable_add_header_above(kable_input, header, bold, italic)) |
Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 34 | } |
| 35 | } |
| 36 | |
| 37 | # HTML |
Hao Zhu | 32f43f7 | 2017-06-20 18:24:54 -0400 | [diff] [blame] | 38 | htmlTable_add_header_above <- function(kable_input, header, bold, italic) { |
Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 39 | if (is.null(header)) return(kable_input) |
Hao Zhu | 909ea38 | 2017-06-12 15:43:47 -0400 | [diff] [blame] | 40 | kable_attrs <- attributes(kable_input) |
Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 41 | kable_xml <- read_xml(as.character(kable_input), options = c("COMPACT")) |
Hao Zhu | 62cdde5 | 2017-05-20 22:16:03 -0400 | [diff] [blame] | 42 | kable_xml_thead <- xml_tpart(kable_xml, "thead") |
Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 43 | |
| 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 Zhu | 32f43f7 | 2017-06-20 18:24:54 -0400 | [diff] [blame] | 54 | new_header_row <- htmlTable_new_header_generator(header, bold, italic) |
Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 55 | xml_add_child(kable_xml_thead, new_header_row, .where = 0) |
Hao Zhu | f2dfd14 | 2017-07-24 14:43:28 -0400 | [diff] [blame^] | 56 | out <- as_kable_xml(kable_xml) |
Hao Zhu | 909ea38 | 2017-06-12 15:43:47 -0400 | [diff] [blame] | 57 | attributes(out) <- kable_attrs |
Hao Zhu | 6a07646 | 2017-03-01 12:59:01 -0500 | [diff] [blame] | 58 | return(out) |
Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | standardize_header_input <- function(header) { |
| 62 | header_names <- names(header) |
| 63 | |
| 64 | if (is.null(header_names)) { |
| 65 | return(data.frame(header = header, colspan = 1, row.names = NULL)) |
| 66 | } |
| 67 | |
| 68 | names(header)[header_names == ""] <- header[header_names == ""] |
| 69 | header[header_names == ""] <- 1 |
| 70 | header_names <- names(header) |
| 71 | header <- as.numeric(header) |
| 72 | names(header) <- header_names |
| 73 | return(data.frame(header = names(header), colspan = header, row.names = NULL)) |
| 74 | } |
| 75 | |
Hao Zhu | 32f43f7 | 2017-06-20 18:24:54 -0400 | [diff] [blame] | 76 | htmlTable_new_header_generator <- function(header_df, bold, italic) { |
| 77 | row_style <- paste0( |
| 78 | ifelse(bold, "font-weight: bold; ", ""), |
| 79 | ifelse(italic, "font-style: italic; ", "") |
| 80 | ) |
Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 81 | header_items <- apply(header_df, 1, function(x) { |
Hao Zhu | acc7ceb | 2017-05-26 10:50:25 -0700 | [diff] [blame] | 82 | if (trimws(x[1]) == "") { |
Hao Zhu | 6d2faa1 | 2017-05-24 02:16:45 -0400 | [diff] [blame] | 83 | paste0('<th style="border-bottom:hidden"></th>') |
| 84 | } else { |
Hao Zhu | acc7ceb | 2017-05-26 10:50:25 -0700 | [diff] [blame] | 85 | paste0('<th style="text-align:center; border-bottom:hidden; ', |
Hao Zhu | 32f43f7 | 2017-06-20 18:24:54 -0400 | [diff] [blame] | 86 | 'padding-bottom:0; padding-left:3px;padding-right:3px;', |
| 87 | row_style, |
| 88 | '" colspan="', |
Hao Zhu | 71e378a | 2017-05-26 11:00:21 -0700 | [diff] [blame] | 89 | x[2], '"><div style="border-bottom: 1px solid #ddd;padding-bottom: 5px;">', |
Hao Zhu | acc7ceb | 2017-05-26 10:50:25 -0700 | [diff] [blame] | 90 | x[1], '</div></th>') |
Hao Zhu | 6d2faa1 | 2017-05-24 02:16:45 -0400 | [diff] [blame] | 91 | } |
Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 92 | }) |
| 93 | header_text <- paste(c("<tr>", header_items, "</tr>"), collapse = "") |
| 94 | header_xml <- read_xml(header_text, options = c("COMPACT")) |
| 95 | return(header_xml) |
| 96 | } |
| 97 | |
| 98 | # Add an extra header row above the current header in a LaTeX table ------ |
Hao Zhu | 32f43f7 | 2017-06-20 18:24:54 -0400 | [diff] [blame] | 99 | pdfTable_add_header_above <- function(kable_input, header, bold, italic) { |
Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 100 | table_info <- magic_mirror(kable_input) |
| 101 | header <- standardize_header_input(header) |
Hao Zhu | 2ce42b9 | 2017-06-15 17:15:33 -0400 | [diff] [blame] | 102 | header$header <- escape_latex(header$header) |
| 103 | header$header <- gsub("\\\\", "\\\\\\\\", header$header) |
Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 104 | hline_type <- switch(table_info$booktabs + 1, "\\\\hline", "\\\\toprule") |
Hao Zhu | 32f43f7 | 2017-06-20 18:24:54 -0400 | [diff] [blame] | 105 | new_header_split <- pdfTable_new_header_generator(header, table_info$booktabs, |
| 106 | bold, italic) |
Hao Zhu | 2ce42b9 | 2017-06-15 17:15:33 -0400 | [diff] [blame] | 107 | new_header <- paste0(new_header_split[1], "\n", new_header_split[2]) |
Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 108 | out <- sub(hline_type, |
Hao Zhu | 2ce42b9 | 2017-06-15 17:15:33 -0400 | [diff] [blame] | 109 | paste0(hline_type, "\n", new_header), |
Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 110 | as.character(kable_input)) |
| 111 | out <- structure(out, format = "latex", class = "knitr_kable") |
Hao Zhu | 2ce42b9 | 2017-06-15 17:15:33 -0400 | [diff] [blame] | 112 | # new_header_row <- latex_contents_escape(new_header_split[1]) |
| 113 | if (is.null(table_info$new_header_row)) { |
| 114 | table_info$new_header_row <- new_header_split[1] |
| 115 | table_info$header_df <- list(header) |
| 116 | } else { |
| 117 | table_info$new_header_row <- c(table_info$new_header_row, new_header_split[1]) |
| 118 | table_info$header_df[[length(table_info$header_df) + 1]] <- header |
| 119 | } |
Hao Zhu | 32f43f7 | 2017-06-20 18:24:54 -0400 | [diff] [blame] | 120 | attr(out, "kable_meta") <- table_info |
Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 121 | return(out) |
| 122 | } |
| 123 | |
Hao Zhu | 32f43f7 | 2017-06-20 18:24:54 -0400 | [diff] [blame] | 124 | pdfTable_new_header_generator <- function(header_df, booktabs = FALSE, |
| 125 | bold, italic) { |
Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 126 | if (booktabs) { |
| 127 | header_df$align <- "c" |
| 128 | } else { |
| 129 | header_df$align <- "|c|" |
| 130 | header_df$align[1] <- "c|" |
| 131 | header_df$align[nrow(header_df)] <- "|c" |
| 132 | } |
| 133 | header_items <- apply(header_df, 1, function(x) { |
| 134 | # if(x[2] == 1) return(x[1]) |
Hao Zhu | 32f43f7 | 2017-06-20 18:24:54 -0400 | [diff] [blame] | 135 | paste0('\\\\multicolumn{', x[2], '}{', x[3], '}{', |
| 136 | ifelse(bold, "\\\\bfseries ", ""), |
| 137 | ifelse(italic, "\\\\em ", ""), |
| 138 | x[1], "}") |
Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 139 | }) |
| 140 | header_text <- paste(paste(header_items, collapse = " & "), "\\\\\\\\") |
Hao Zhu | 2ce42b9 | 2017-06-15 17:15:33 -0400 | [diff] [blame] | 141 | cline <- cline_gen(header_df, booktabs) |
| 142 | return(c(header_text, cline)) |
| 143 | } |
| 144 | |
| 145 | cline_gen <- function(header_df, booktabs) { |
Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 146 | cline_end <- cumsum(header_df$colspan) |
| 147 | cline_start <- c(0, cline_end) + 1 |
| 148 | cline_start <- cline_start[-length(cline_start)] |
Hao Zhu | c05e181 | 2017-02-25 01:45:35 -0500 | [diff] [blame] | 149 | cline_type <- switch(booktabs + 1, |
| 150 | "\\\\cline{", |
| 151 | "\\\\cmidrule(l{2pt}r{2pt}){") |
Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 152 | cline <- paste0(cline_type, cline_start, "-", cline_end, "}") |
| 153 | cline <- cline[trimws(header_df$header) != ""] |
| 154 | cline <- paste(cline, collapse = " ") |
Hao Zhu | 2ce42b9 | 2017-06-15 17:15:33 -0400 | [diff] [blame] | 155 | return(cline) |
Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 156 | } |
Hao Zhu | 2ce42b9 | 2017-06-15 17:15:33 -0400 | [diff] [blame] | 157 | |
| 158 | |