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