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)`. |
jokorn | a00ad66 | 2019-11-07 21:11:19 +0100 | [diff] [blame] | 13 | #' Alternatively, a data frame with two columns can be provided: The first |
| 14 | #' column should contain the header names (character vector) and the second |
| 15 | #' column should contain the colspan (numeric vector). This input can be used |
| 16 | #' if there are problems with unicode characters in the headers. |
Hao Zhu | 32f43f7 | 2017-06-20 18:24:54 -0400 | [diff] [blame] | 17 | #' @param bold A T/F value to control whether the text should be bolded. |
| 18 | #' @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] | 19 | #' @param monospace A T/F value to control whether the text of the selected column |
| 20 | #' need to be monospaced (verbatim) |
Hao Zhu | 76f0eb6 | 2018-09-15 12:38:33 -0400 | [diff] [blame] | 21 | #' @param underline A T/F value to control whether the text of the selected row |
| 22 | #' need to be underlined |
| 23 | #' @param strikeout A T/F value to control whether the text of the selected row |
| 24 | #' need to be stricked out. |
Hao Zhu | 3465b50 | 2018-04-02 22:41:46 -0400 | [diff] [blame] | 25 | #' @param align A character string for cell alignment. For HTML, possible values could |
| 26 | #' be `l`, `c`, `r` plus `left`, `center`, `right`, `justify`, `initial` and `inherit` |
| 27 | #' while for LaTeX, you can only choose from `l`, `c` & `r`. |
Hao Zhu | 76f0eb6 | 2018-09-15 12:38:33 -0400 | [diff] [blame] | 28 | #' @param color A character string/vector for text color. Here please pay |
| 29 | #' attention to the differences in color codes between HTML and LaTeX. |
| 30 | #' @param background A character string/vector for background color. Here please |
| 31 | #' pay attention to the differences in color codes between HTML and LaTeX. Also |
| 32 | #' note that in HTML, background defined in cell_spec won't cover the whole |
| 33 | #' cell. |
| 34 | #' @param font_size A numeric input/vector for font size. For HTML, you can also use |
| 35 | #' options including `xx-small`, `x-small`, `small`, `medium`, `large`, |
| 36 | #' `x-large`, `xx-large`, `smaller`, `larger`, `initial` and `inherit`. |
| 37 | #' @param angle 0-360, degree that the text will rotate. |
Hao Zhu | ac7e70f | 2017-08-02 00:18:36 -0400 | [diff] [blame] | 38 | #' @param escape A T/F value showing whether special characters should be |
| 39 | #' escaped. |
Salzer | 6bc84f8 | 2018-02-11 13:18:01 -0500 | [diff] [blame] | 40 | #' @param line A T/F value to control whether a line will appear underneath the |
| 41 | #' header |
Hao Zhu | b548aac | 2018-09-18 13:29:01 -0400 | [diff] [blame] | 42 | #' @param line_sep A numeric value indicating how much the midlines should be |
| 43 | #' separated by space. Default is 3. |
Hao Zhu | 6d68f7c | 2018-09-15 12:44:15 -0400 | [diff] [blame] | 44 | #' @param extra_css An HTML only option. CSS defined here will be send to the |
| 45 | #' td cell. |
Hao Zhu | f64f475 | 2018-10-23 10:35:16 -0400 | [diff] [blame] | 46 | #' @param include_empty Whether empty cells in HTML should also be styled. |
| 47 | #' Default is FALSE. |
Hao Zhu | 8b2126e | 2019-04-16 17:58:01 -0400 | [diff] [blame] | 48 | #' @param border_left T/F option for border on the left side in latex. |
| 49 | #' @param border_right T/F option for border on the right side in latex. |
Hao Zhu | f7994dd | 2017-02-27 16:58:42 -0500 | [diff] [blame] | 50 | #' |
Hao Zhu | 78e6122 | 2017-05-24 20:53:35 -0400 | [diff] [blame] | 51 | #' @examples x <- knitr::kable(head(mtcars), "html") |
| 52 | #' # Add a row of header with 3 columns on the top of the table. The column |
| 53 | #' # span for the 2nd and 3rd one are 5 & 6. |
| 54 | #' add_header_above(x, c(" ", "Group 1" = 5, "Group 2" = 6)) |
| 55 | #' |
Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 56 | #' @export |
Hao Zhu | ac7e70f | 2017-08-02 00:18:36 -0400 | [diff] [blame] | 57 | add_header_above <- function(kable_input, header = NULL, |
Hao Zhu | 76f0eb6 | 2018-09-15 12:38:33 -0400 | [diff] [blame] | 58 | bold = FALSE, italic = FALSE, monospace = FALSE, |
| 59 | underline = FALSE, strikeout = FALSE, |
| 60 | align = "c", color = NULL, background = NULL, |
| 61 | font_size = NULL, angle = NULL, |
Hao Zhu | b548aac | 2018-09-18 13:29:01 -0400 | [diff] [blame] | 62 | escape = TRUE, line = TRUE, line_sep = 3, |
Hao Zhu | 8b2126e | 2019-04-16 17:58:01 -0400 | [diff] [blame] | 63 | extra_css = NULL, include_empty = FALSE, |
| 64 | border_left = FALSE, border_right = FALSE) { |
Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 65 | kable_format <- attr(kable_input, "format") |
| 66 | if (!kable_format %in% c("html", "latex")) { |
Hao Zhu | 401ebd8 | 2018-01-14 17:10:20 -0500 | [diff] [blame] | 67 | warning("Please specify format in kable. kableExtra can customize either ", |
Hao Zhu | 76f0eb6 | 2018-09-15 12:38:33 -0400 | [diff] [blame] | 68 | "HTML or LaTeX outputs. See https://haozhu233.github.io/kableExtra/ ", |
| 69 | "for details.") |
Hao Zhu | 401ebd8 | 2018-01-14 17:10:20 -0500 | [diff] [blame] | 70 | return(kable_input) |
Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 71 | } |
| 72 | if (kable_format == "html") { |
Hao Zhu | f64f475 | 2018-10-23 10:35:16 -0400 | [diff] [blame] | 73 | return(htmlTable_add_header_above( |
| 74 | kable_input, header, bold, italic, monospace, underline, strikeout, |
| 75 | align, color, background, font_size, angle, escape, line, line_sep, |
| 76 | extra_css, include_empty |
| 77 | )) |
Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 78 | } |
| 79 | if (kable_format == "latex") { |
Hao Zhu | f64f475 | 2018-10-23 10:35:16 -0400 | [diff] [blame] | 80 | return(pdfTable_add_header_above( |
| 81 | kable_input, header, bold, italic, monospace, underline, strikeout, |
Hao Zhu | 8b2126e | 2019-04-16 17:58:01 -0400 | [diff] [blame] | 82 | align, color, background, font_size, angle, escape, line, line_sep, |
| 83 | border_left, border_right)) |
Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 84 | } |
| 85 | } |
| 86 | |
| 87 | # HTML |
Hao Zhu | 3465b50 | 2018-04-02 22:41:46 -0400 | [diff] [blame] | 88 | htmlTable_add_header_above <- function(kable_input, header, bold, italic, |
Hao Zhu | 76f0eb6 | 2018-09-15 12:38:33 -0400 | [diff] [blame] | 89 | monospace, underline, strikeout, |
Hao Zhu | 6d68f7c | 2018-09-15 12:44:15 -0400 | [diff] [blame] | 90 | align, color, background, font_size, |
Hao Zhu | b548aac | 2018-09-18 13:29:01 -0400 | [diff] [blame] | 91 | angle, escape, line, line_sep, |
Hao Zhu | f64f475 | 2018-10-23 10:35:16 -0400 | [diff] [blame] | 92 | extra_css, include_empty) { |
Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 93 | if (is.null(header)) return(kable_input) |
jokorn | a00ad66 | 2019-11-07 21:11:19 +0100 | [diff] [blame] | 94 | |
Hao Zhu | 909ea38 | 2017-06-12 15:43:47 -0400 | [diff] [blame] | 95 | kable_attrs <- attributes(kable_input) |
Hao Zhu | 558c72f | 2017-07-24 15:12:00 -0400 | [diff] [blame] | 96 | kable_xml <- read_kable_as_xml(kable_input) |
Hao Zhu | 62cdde5 | 2017-05-20 22:16:03 -0400 | [diff] [blame] | 97 | kable_xml_thead <- xml_tpart(kable_xml, "thead") |
jokorn | a00ad66 | 2019-11-07 21:11:19 +0100 | [diff] [blame] | 98 | |
| 99 | if (is.data.frame(header)){ |
jokorn | 1cb595e | 2019-11-07 21:26:54 +0100 | [diff] [blame] | 100 | if(ncol(header) == 2 & is.character(header[[1]]) & is.numeric(header[[2]])){ |
jokorn | 19f6b53 | 2019-11-07 21:57:53 +0100 | [diff] [blame] | 101 | header <- data.frame(header = header[[1]], colspan = header[[2]], |
| 102 | stringsAsFactors = FALSE) |
jokorn | a00ad66 | 2019-11-07 21:11:19 +0100 | [diff] [blame] | 103 | } |
| 104 | else { |
| 105 | stop("If header input is provided as a data frame instead of a named vector ", |
| 106 | "it must consist of only two columns: ", |
| 107 | "The first should be a character vector with ", |
| 108 | "header names and the second should be a numeric vector with ", |
| 109 | "the number of columns the header should span.") |
| 110 | } |
| 111 | } |
| 112 | else { |
| 113 | header <- standardize_header_input(header) |
| 114 | } |
| 115 | |
Hao Zhu | ac7e70f | 2017-08-02 00:18:36 -0400 | [diff] [blame] | 116 | if (escape) { |
| 117 | header$header <- escape_html(header$header) |
| 118 | } |
| 119 | |
Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 120 | header_rows <- xml_children(kable_xml_thead) |
| 121 | bottom_header_row <- header_rows[[length(header_rows)]] |
| 122 | kable_ncol <- length(xml_children(bottom_header_row)) |
| 123 | if (sum(header$colspan) != kable_ncol) { |
| 124 | stop("The new header row you provided has a different total number of ", |
| 125 | "columns with the original kable output.") |
| 126 | } |
| 127 | |
Hao Zhu | 76f0eb6 | 2018-09-15 12:38:33 -0400 | [diff] [blame] | 128 | new_header_row <- htmlTable_new_header_generator( |
Hao Zhu | b548aac | 2018-09-18 13:29:01 -0400 | [diff] [blame] | 129 | header, bold, italic, monospace, underline, strikeout, align, |
Hao Zhu | f64f475 | 2018-10-23 10:35:16 -0400 | [diff] [blame] | 130 | color, background, font_size, angle, line, line_sep, extra_css, include_empty |
Hao Zhu | 76f0eb6 | 2018-09-15 12:38:33 -0400 | [diff] [blame] | 131 | ) |
Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 132 | xml_add_child(kable_xml_thead, new_header_row, .where = 0) |
Hao Zhu | f2dfd14 | 2017-07-24 14:43:28 -0400 | [diff] [blame] | 133 | out <- as_kable_xml(kable_xml) |
Hao Zhu | 2345676 | 2018-03-26 12:30:10 -0400 | [diff] [blame] | 134 | if (is.null(kable_attrs$header_above)) { |
| 135 | kable_attrs$header_above <- 1 |
| 136 | } else { |
| 137 | kable_attrs$header_above <- kable_attrs$header_above + 1 |
| 138 | } |
Hao Zhu | 909ea38 | 2017-06-12 15:43:47 -0400 | [diff] [blame] | 139 | attributes(out) <- kable_attrs |
Hao Zhu | f210083 | 2018-01-11 16:20:29 -0500 | [diff] [blame] | 140 | if (!"kableExtra" %in% class(out)) class(out) <- c("kableExtra", class(out)) |
Hao Zhu | 6a07646 | 2017-03-01 12:59:01 -0500 | [diff] [blame] | 141 | return(out) |
Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | standardize_header_input <- function(header) { |
| 145 | header_names <- names(header) |
| 146 | |
| 147 | if (is.null(header_names)) { |
| 148 | return(data.frame(header = header, colspan = 1, row.names = NULL)) |
| 149 | } |
| 150 | |
| 151 | names(header)[header_names == ""] <- header[header_names == ""] |
| 152 | header[header_names == ""] <- 1 |
| 153 | header_names <- names(header) |
| 154 | header <- as.numeric(header) |
| 155 | names(header) <- header_names |
martinschmelzer | f43dcf5 | 2019-01-08 13:02:52 +0100 | [diff] [blame] | 156 | return(data.frame(header = names(header), colspan = header, row.names = NULL, stringsAsFactors = F)) |
Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 157 | } |
| 158 | |
Hao Zhu | 76f0eb6 | 2018-09-15 12:38:33 -0400 | [diff] [blame] | 159 | htmlTable_new_header_generator <- function(header_df, bold, italic, monospace, |
Hao Zhu | b548aac | 2018-09-18 13:29:01 -0400 | [diff] [blame] | 160 | underline, strikeout, align, |
Hao Zhu | 76f0eb6 | 2018-09-15 12:38:33 -0400 | [diff] [blame] | 161 | color, background, font_size, |
Hao Zhu | f64f475 | 2018-10-23 10:35:16 -0400 | [diff] [blame] | 162 | angle, line, line_sep, extra_css, |
| 163 | include_empty) { |
Hao Zhu | 3465b50 | 2018-04-02 22:41:46 -0400 | [diff] [blame] | 164 | if (align %in% c("l", "c", "r")) { |
| 165 | align <- switch(align, r = "right", c = "center", l = "left") |
| 166 | } |
Hao Zhu | 32f43f7 | 2017-06-20 18:24:54 -0400 | [diff] [blame] | 167 | row_style <- paste0( |
Hao Zhu | 3465b50 | 2018-04-02 22:41:46 -0400 | [diff] [blame] | 168 | paste0("text-align: ", align, "; "), |
Hao Zhu | 32f43f7 | 2017-06-20 18:24:54 -0400 | [diff] [blame] | 169 | ifelse(bold, "font-weight: bold; ", ""), |
Hao Zhu | ac7e70f | 2017-08-02 00:18:36 -0400 | [diff] [blame] | 170 | ifelse(italic, "font-style: italic; ", ""), |
Hao Zhu | 76f0eb6 | 2018-09-15 12:38:33 -0400 | [diff] [blame] | 171 | ifelse(monospace, "font-family: monospace; ", ""), |
| 172 | ifelse(underline, "text-decoration: underline; ", ""), |
| 173 | ifelse(strikeout, "text-decoration: line-through; ", "") |
Hao Zhu | 32f43f7 | 2017-06-20 18:24:54 -0400 | [diff] [blame] | 174 | ) |
Hao Zhu | 76f0eb6 | 2018-09-15 12:38:33 -0400 | [diff] [blame] | 175 | if (!is.null(color)) { |
Hao Zhu | 72917f9 | 2019-03-15 18:41:42 -0400 | [diff] [blame] | 176 | row_style <- paste0(row_style, "color: ", html_color(color), " !important;") |
Hao Zhu | 76f0eb6 | 2018-09-15 12:38:33 -0400 | [diff] [blame] | 177 | } |
| 178 | if (!is.null(background)) { |
| 179 | row_style <- paste0( |
| 180 | row_style, |
| 181 | "padding-right: 4px; padding-left: 4px; ", |
Hao Zhu | 72917f9 | 2019-03-15 18:41:42 -0400 | [diff] [blame] | 182 | "background-color: ", html_color(background), " !important;" |
Hao Zhu | 76f0eb6 | 2018-09-15 12:38:33 -0400 | [diff] [blame] | 183 | ) |
| 184 | } |
| 185 | if (!is.null(font_size)) { |
| 186 | if (is.numeric(font_size)) font_size <- paste0(font_size, "px") |
| 187 | row_style <- paste0(row_style, "font-size: ", font_size, ";") |
| 188 | } |
Hao Zhu | 6d68f7c | 2018-09-15 12:44:15 -0400 | [diff] [blame] | 189 | if (!is.null(extra_css)) { |
| 190 | row_style <- paste0(row_style, extra_css) |
| 191 | } |
| 192 | |
Hao Zhu | 76f0eb6 | 2018-09-15 12:38:33 -0400 | [diff] [blame] | 193 | if (!is.null(angle)) { |
| 194 | angle <- paste0("-webkit-transform: rotate(", angle, |
| 195 | "deg); -moz-transform: rotate(", angle, |
| 196 | "deg); -ms-transform: rotate(", angle, |
| 197 | "deg); -o-transform: rotate(", angle, |
| 198 | "deg); transform: rotate(", angle, |
| 199 | "deg); display: inline-block; ") |
Hao Zhu | f64f475 | 2018-10-23 10:35:16 -0400 | [diff] [blame] | 200 | if (include_empty) { |
| 201 | header_df$header <- paste0('<span style="', angle, '">', |
| 202 | header_df$header, '</span>') |
| 203 | } else { |
| 204 | header_df$header <- ifelse( |
| 205 | trimws(header_df$header) == "", |
| 206 | header_df$header, |
| 207 | paste0('<span style="', angle, '">', header_df$header, '</span>') |
| 208 | ) |
| 209 | } |
Hao Zhu | 76f0eb6 | 2018-09-15 12:38:33 -0400 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | line <- ifelse(ez_rep(line, nrow(header_df)), |
| 213 | "border-bottom: 1px solid #ddd; padding-bottom: 5px; ", "") |
Hao Zhu | b548aac | 2018-09-18 13:29:01 -0400 | [diff] [blame] | 214 | line_sep <- ez_rep(line_sep, nrow(header_df)) |
| 215 | line_sep <- glue::glue('padding-left:{line_sep}px;padding-right:{line_sep}px;') |
Hao Zhu | 76f0eb6 | 2018-09-15 12:38:33 -0400 | [diff] [blame] | 216 | |
| 217 | header_items <- ifelse( |
| 218 | trimws(header_df$header) == "", |
| 219 | paste0('<th style="border-bottom:hidden" colspan="', header_df$colspan, |
| 220 | '"></th>'), |
| 221 | paste0( |
Hao Zhu | b548aac | 2018-09-18 13:29:01 -0400 | [diff] [blame] | 222 | '<th style="border-bottom:hidden; padding-bottom:0; ', |
| 223 | line_sep, row_style, '" colspan="', |
Hao Zhu | 76f0eb6 | 2018-09-15 12:38:33 -0400 | [diff] [blame] | 224 | header_df$colspan, '"><div style="', line, '">', header_df$header, |
| 225 | '</div></th>') |
| 226 | ) |
Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 227 | header_text <- paste(c("<tr>", header_items, "</tr>"), collapse = "") |
| 228 | header_xml <- read_xml(header_text, options = c("COMPACT")) |
| 229 | return(header_xml) |
| 230 | } |
| 231 | |
| 232 | # 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] | 233 | pdfTable_add_header_above <- function(kable_input, header, bold, italic, |
Hao Zhu | b548aac | 2018-09-18 13:29:01 -0400 | [diff] [blame] | 234 | monospace, underline, strikeout, align, |
| 235 | color, background, font_size, angle, |
Hao Zhu | 8b2126e | 2019-04-16 17:58:01 -0400 | [diff] [blame] | 236 | escape, line, line_sep, |
| 237 | border_left, border_right) { |
Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 238 | table_info <- magic_mirror(kable_input) |
jokorn | a00ad66 | 2019-11-07 21:11:19 +0100 | [diff] [blame] | 239 | |
| 240 | if (is.data.frame(header)){ |
jokorn | 1cb595e | 2019-11-07 21:26:54 +0100 | [diff] [blame] | 241 | if(ncol(header) == 2 & is.character(header[[1]]) & is.numeric(header[[2]])){ |
jokorn | 19f6b53 | 2019-11-07 21:57:53 +0100 | [diff] [blame] | 242 | header <- data.frame(header = header[[1]], colspan = header[[2]], |
| 243 | stringsAsFactors = FALSE) |
jokorn | a00ad66 | 2019-11-07 21:11:19 +0100 | [diff] [blame] | 244 | } |
| 245 | else { |
| 246 | stop("If header input is provided as a data frame instead of a named vector ", |
| 247 | "it must consist of only two columns: ", |
| 248 | "The first should be a character vector with ", |
| 249 | "header names and the second should be a numeric vector with ", |
| 250 | "the number of columns the header should span.") |
| 251 | } |
| 252 | } |
| 253 | else { |
| 254 | header <- standardize_header_input(header) |
| 255 | } |
Hao Zhu | 248bbef | 2018-04-02 18:25:14 -0400 | [diff] [blame] | 256 | |
Hao Zhu | ac7e70f | 2017-08-02 00:18:36 -0400 | [diff] [blame] | 257 | if (escape) { |
Hao Zhu | f94a26f | 2018-04-05 17:42:55 -0400 | [diff] [blame] | 258 | header$header <- input_escape(header$header, align) |
Hao Zhu | ac7e70f | 2017-08-02 00:18:36 -0400 | [diff] [blame] | 259 | } |
Hao Zhu | 248bbef | 2018-04-02 18:25:14 -0400 | [diff] [blame] | 260 | |
Hao Zhu | 3465b50 | 2018-04-02 22:41:46 -0400 | [diff] [blame] | 261 | align <- match.arg(align, c("c", "l", "r")) |
Hao Zhu | 248bbef | 2018-04-02 18:25:14 -0400 | [diff] [blame] | 262 | |
Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 263 | hline_type <- switch(table_info$booktabs + 1, "\\\\hline", "\\\\toprule") |
Hao Zhu | 3465b50 | 2018-04-02 22:41:46 -0400 | [diff] [blame] | 264 | new_header_split <- pdfTable_new_header_generator( |
Hao Zhu | 76f0eb6 | 2018-09-15 12:38:33 -0400 | [diff] [blame] | 265 | header, table_info$booktabs, bold, italic, monospace, underline, strikeout, |
Hao Zhu | 8b2126e | 2019-04-16 17:58:01 -0400 | [diff] [blame] | 266 | align, color, background, font_size, angle, line_sep, |
| 267 | border_left, border_right) |
Hao Zhu | 76f0eb6 | 2018-09-15 12:38:33 -0400 | [diff] [blame] | 268 | if (line) { |
Salzer | 6bc84f8 | 2018-02-11 13:18:01 -0500 | [diff] [blame] | 269 | new_header <- paste0(new_header_split[1], "\n", new_header_split[2]) |
| 270 | } else { |
| 271 | new_header <- new_header_split[1] |
| 272 | } |
Hao Zhu | 3fc0e88 | 2018-04-03 16:06:41 -0400 | [diff] [blame] | 273 | out <- str_replace(solve_enc(kable_input), |
Hao Zhu | b2b4199 | 2017-10-03 12:50:03 -0400 | [diff] [blame] | 274 | hline_type, |
| 275 | paste0(hline_type, "\n", new_header)) |
Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 276 | out <- structure(out, format = "latex", class = "knitr_kable") |
Hao Zhu | 2ce42b9 | 2017-06-15 17:15:33 -0400 | [diff] [blame] | 277 | # new_header_row <- latex_contents_escape(new_header_split[1]) |
| 278 | if (is.null(table_info$new_header_row)) { |
| 279 | table_info$new_header_row <- new_header_split[1] |
| 280 | table_info$header_df <- list(header) |
| 281 | } else { |
| 282 | table_info$new_header_row <- c(table_info$new_header_row, new_header_split[1]) |
| 283 | table_info$header_df[[length(table_info$header_df) + 1]] <- header |
| 284 | } |
Hao Zhu | 32f43f7 | 2017-06-20 18:24:54 -0400 | [diff] [blame] | 285 | attr(out, "kable_meta") <- table_info |
Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 286 | return(out) |
| 287 | } |
| 288 | |
Hao Zhu | 76f0eb6 | 2018-09-15 12:38:33 -0400 | [diff] [blame] | 289 | ez_rep <- function(x, n) { |
| 290 | if (is.null(x)) return(NULL) |
| 291 | if (length(x) == 1) return(rep(x, n)) |
| 292 | return(x) |
| 293 | } |
| 294 | |
Hao Zhu | 32f43f7 | 2017-06-20 18:24:54 -0400 | [diff] [blame] | 295 | pdfTable_new_header_generator <- function(header_df, booktabs = FALSE, |
Hao Zhu | 76f0eb6 | 2018-09-15 12:38:33 -0400 | [diff] [blame] | 296 | bold, italic, monospace, |
| 297 | underline, strikeout, align, |
Hao Zhu | b548aac | 2018-09-18 13:29:01 -0400 | [diff] [blame] | 298 | color, background, font_size, angle, |
Hao Zhu | 8b2126e | 2019-04-16 17:58:01 -0400 | [diff] [blame] | 299 | line_sep, border_left, border_right) { |
Hao Zhu | 76f0eb6 | 2018-09-15 12:38:33 -0400 | [diff] [blame] | 300 | n <- nrow(header_df) |
| 301 | bold <- ez_rep(bold, n) |
| 302 | italic <- ez_rep(italic, n) |
| 303 | monospace <- ez_rep(monospace, n) |
| 304 | underline <- ez_rep(underline, n) |
| 305 | strikeout <- ez_rep(strikeout, n) |
| 306 | align <- ez_rep(align, n) |
| 307 | color <- ez_rep(color, n) |
| 308 | background <- ez_rep(background, n) |
| 309 | font_size <- ez_rep(font_size, n) |
| 310 | angle <- ez_rep(angle, n) |
Hao Zhu | 8b2126e | 2019-04-16 17:58:01 -0400 | [diff] [blame] | 311 | if (!booktabs & n != 1) { |
martinschmelzer | f43dcf5 | 2019-01-08 13:02:52 +0100 | [diff] [blame] | 312 | align[1:(n - 1)] <- paste0(align[1:(n - 1)], "|") |
Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 313 | } |
Hao Zhu | 8b2126e | 2019-04-16 17:58:01 -0400 | [diff] [blame] | 314 | if (border_left) { |
| 315 | align[1] <- paste0("|", align[1]) |
| 316 | } |
| 317 | if (border_right) { |
| 318 | align[n] <- paste0(align[n], "|") |
| 319 | } |
| 320 | |
Hao Zhu | 76f0eb6 | 2018-09-15 12:38:33 -0400 | [diff] [blame] | 321 | header <- header_df$header |
| 322 | colspan <- header_df$colspan |
| 323 | |
| 324 | header <- ifelse(bold, paste0('\\\\textbf\\{', header, '\\}'), header) |
| 325 | header <- ifelse(italic, paste0('\\\\em\\{', header, '\\}'), header) |
| 326 | header <- ifelse(monospace, paste0('\\\\ttfamily\\{', header, '\\}'), header) |
| 327 | header <- ifelse(underline, paste0('\\\\underline\\{', header, '\\}'), header) |
| 328 | header <- ifelse(strikeout, paste0('\\\\sout\\{', header, '\\}'), header) |
| 329 | if (!is.null(color)) { |
| 330 | color <- latex_color(color) |
| 331 | header <- paste0("\\\\textcolor", color, "\\{", header, "\\}") |
| 332 | } |
| 333 | if (!is.null(background)) { |
| 334 | background <- latex_color(background) |
| 335 | header <- paste0("\\\\cellcolor", background, "\\{", header, "\\}") |
| 336 | } |
| 337 | if (!is.null(font_size)) { |
| 338 | header <- paste0("\\\\bgroup\\\\fontsize\\{", font_size, "\\}\\{", |
| 339 | as.numeric(font_size) + 2, |
| 340 | "\\}\\\\selectfont ", header, "\\\\egroup\\{\\}") |
| 341 | } |
| 342 | if (!is.null(angle)) { |
| 343 | header <- paste0("\\\\rotatebox\\{", angle, "\\}\\{", header, "\\}") |
| 344 | } |
| 345 | header_items <- paste0( |
| 346 | '\\\\multicolumn\\{', colspan, '\\}\\{', align, '\\}\\{', header, '\\}' |
| 347 | ) |
| 348 | |
Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 349 | header_text <- paste(paste(header_items, collapse = " & "), "\\\\\\\\") |
Hao Zhu | b548aac | 2018-09-18 13:29:01 -0400 | [diff] [blame] | 350 | cline <- cline_gen(header_df, booktabs, line_sep) |
Hao Zhu | 2ce42b9 | 2017-06-15 17:15:33 -0400 | [diff] [blame] | 351 | return(c(header_text, cline)) |
| 352 | } |
| 353 | |
Hao Zhu | b548aac | 2018-09-18 13:29:01 -0400 | [diff] [blame] | 354 | cline_gen <- function(header_df, booktabs, line_sep) { |
Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 355 | cline_end <- cumsum(header_df$colspan) |
| 356 | cline_start <- c(0, cline_end) + 1 |
| 357 | cline_start <- cline_start[-length(cline_start)] |
Hao Zhu | b548aac | 2018-09-18 13:29:01 -0400 | [diff] [blame] | 358 | cline_type <- switch( |
| 359 | booktabs + 1, |
| 360 | "\\\\cline{", |
| 361 | glue::glue("\\\\cmidrule(l{[line_sep]pt}r{[line_sep]pt}){", |
| 362 | .open = "[", .close = "]")) |
Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 363 | cline <- paste0(cline_type, cline_start, "-", cline_end, "}") |
| 364 | cline <- cline[trimws(header_df$header) != ""] |
| 365 | cline <- paste(cline, collapse = " ") |
Hao Zhu | 2ce42b9 | 2017-06-15 17:15:33 -0400 | [diff] [blame] | 366 | return(cline) |
Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 367 | } |
Hao Zhu | 2ce42b9 | 2017-06-15 17:15:33 -0400 | [diff] [blame] | 368 | |
| 369 | |