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