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