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