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 |
Hao Zhu | 7012cd0 | 2020-08-09 15:40:50 -0400 | [diff] [blame] | 14 | #' column should contain the header names (character vector) and the second |
jokorn | a00ad66 | 2019-11-07 21:11:19 +0100 | [diff] [blame] | 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 | 9399dcc | 2020-08-26 17:27:38 -0400 | [diff] [blame] | 51 | #' @examples |
| 52 | #' \dontrun{ |
| 53 | #' x <- knitr::kable(head(mtcars), "html") |
Hao Zhu | 78e6122 | 2017-05-24 20:53:35 -0400 | [diff] [blame] | 54 | #' # Add a row of header with 3 columns on the top of the table. The column |
| 55 | #' # span for the 2nd and 3rd one are 5 & 6. |
| 56 | #' add_header_above(x, c(" ", "Group 1" = 5, "Group 2" = 6)) |
Hao Zhu | 9399dcc | 2020-08-26 17:27:38 -0400 | [diff] [blame] | 57 | #' } |
Hao Zhu | 78e6122 | 2017-05-24 20:53:35 -0400 | [diff] [blame] | 58 | #' |
Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 59 | #' @export |
Hao Zhu | ac7e70f | 2017-08-02 00:18:36 -0400 | [diff] [blame] | 60 | add_header_above <- function(kable_input, header = NULL, |
Hao Zhu | 76f0eb6 | 2018-09-15 12:38:33 -0400 | [diff] [blame] | 61 | bold = FALSE, italic = FALSE, monospace = FALSE, |
| 62 | underline = FALSE, strikeout = FALSE, |
| 63 | align = "c", color = NULL, background = NULL, |
| 64 | font_size = NULL, angle = NULL, |
Hao Zhu | b548aac | 2018-09-18 13:29:01 -0400 | [diff] [blame] | 65 | escape = TRUE, line = TRUE, line_sep = 3, |
Hao Zhu | 8b2126e | 2019-04-16 17:58:01 -0400 | [diff] [blame] | 66 | extra_css = NULL, include_empty = FALSE, |
| 67 | border_left = FALSE, border_right = FALSE) { |
kbrevoort | cd16bc5 | 2019-09-03 21:25:33 -0400 | [diff] [blame] | 68 | if (is.null(header)) return(kable_input) |
| 69 | |
Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 70 | kable_format <- attr(kable_input, "format") |
| 71 | if (!kable_format %in% c("html", "latex")) { |
Hao Zhu | 401ebd8 | 2018-01-14 17:10:20 -0500 | [diff] [blame] | 72 | warning("Please specify format in kable. kableExtra can customize either ", |
Hao Zhu | 76f0eb6 | 2018-09-15 12:38:33 -0400 | [diff] [blame] | 73 | "HTML or LaTeX outputs. See https://haozhu233.github.io/kableExtra/ ", |
| 74 | "for details.") |
Hao Zhu | 401ebd8 | 2018-01-14 17:10:20 -0500 | [diff] [blame] | 75 | return(kable_input) |
Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 76 | } |
kbrevoort | c79c5a8 | 2019-08-26 22:44:01 -0400 | [diff] [blame] | 77 | if ((length(align) != 1L) & (length(align) != length(header))) { |
| 78 | warning("Length of align vector supplied to add_header_above must either be 1 ", |
| 79 | "or the same length as the header supplied. The length of the align ", |
| 80 | sprintf("vector supplied is %i and the header length is %i.", |
| 81 | length(align), length(header)), |
| 82 | "Using default of centering each element of row.") |
| 83 | align <- 'c' |
| 84 | } |
Hao Zhu | 7012cd0 | 2020-08-09 15:40:50 -0400 | [diff] [blame] | 85 | if (is.null(header)) return(kable_input) |
| 86 | if (is.data.frame(header)){ |
| 87 | if(ncol(header) == 2 & is.character(header[[1]]) & is.numeric(header[[2]])){ |
| 88 | header <- data.frame(header = header[[1]], colspan = header[[2]], |
| 89 | stringsAsFactors = FALSE) |
| 90 | } |
| 91 | else { |
| 92 | stop("If header input is provided as a data frame instead of a named", |
| 93 | "vector it must consist of only two columns: ", |
| 94 | "The first should be a character vector with ", |
| 95 | "header names and the second should be a numeric vector with ", |
| 96 | "the number of columns the header should span.") |
| 97 | } |
| 98 | } |
| 99 | else { |
| 100 | header <- standardize_header_input(header) |
| 101 | } |
Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 102 | if (kable_format == "html") { |
Hao Zhu | f64f475 | 2018-10-23 10:35:16 -0400 | [diff] [blame] | 103 | return(htmlTable_add_header_above( |
| 104 | kable_input, header, bold, italic, monospace, underline, strikeout, |
| 105 | align, color, background, font_size, angle, escape, line, line_sep, |
| 106 | extra_css, include_empty |
| 107 | )) |
Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 108 | } |
| 109 | if (kable_format == "latex") { |
Hao Zhu | f64f475 | 2018-10-23 10:35:16 -0400 | [diff] [blame] | 110 | return(pdfTable_add_header_above( |
| 111 | kable_input, header, bold, italic, monospace, underline, strikeout, |
Hao Zhu | 8b2126e | 2019-04-16 17:58:01 -0400 | [diff] [blame] | 112 | align, color, background, font_size, angle, escape, line, line_sep, |
| 113 | border_left, border_right)) |
Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 114 | } |
| 115 | } |
| 116 | |
| 117 | # HTML |
Hao Zhu | 3465b50 | 2018-04-02 22:41:46 -0400 | [diff] [blame] | 118 | htmlTable_add_header_above <- function(kable_input, header, bold, italic, |
Hao Zhu | 76f0eb6 | 2018-09-15 12:38:33 -0400 | [diff] [blame] | 119 | monospace, underline, strikeout, |
Hao Zhu | 6d68f7c | 2018-09-15 12:44:15 -0400 | [diff] [blame] | 120 | align, color, background, font_size, |
Hao Zhu | b548aac | 2018-09-18 13:29:01 -0400 | [diff] [blame] | 121 | angle, escape, line, line_sep, |
Hao Zhu | f64f475 | 2018-10-23 10:35:16 -0400 | [diff] [blame] | 122 | extra_css, include_empty) { |
Hao Zhu | 909ea38 | 2017-06-12 15:43:47 -0400 | [diff] [blame] | 123 | kable_attrs <- attributes(kable_input) |
Hao Zhu | 558c72f | 2017-07-24 15:12:00 -0400 | [diff] [blame] | 124 | kable_xml <- read_kable_as_xml(kable_input) |
Hao Zhu | 62cdde5 | 2017-05-20 22:16:03 -0400 | [diff] [blame] | 125 | kable_xml_thead <- xml_tpart(kable_xml, "thead") |
Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 126 | |
Hao Zhu | ac7e70f | 2017-08-02 00:18:36 -0400 | [diff] [blame] | 127 | if (escape) { |
| 128 | header$header <- escape_html(header$header) |
| 129 | } |
| 130 | |
kbrevoort | 970777b | 2019-09-05 23:26:44 -0400 | [diff] [blame] | 131 | # If there is no existing header, add one |
| 132 | if (is.null(kable_xml_thead)) { |
Hao Zhu | 69fa19d | 2020-08-12 08:31:14 -0400 | [diff] [blame] | 133 | # Add thead node as first child |
| 134 | xml_add_child(kable_xml, 'thead', .where = 0) |
kbrevoort | 970777b | 2019-09-05 23:26:44 -0400 | [diff] [blame] | 135 | kable_xml_thead <- xml_tpart(kable_xml, 'thead') |
| 136 | |
Hao Zhu | 69fa19d | 2020-08-12 08:31:14 -0400 | [diff] [blame] | 137 | # To check the number of columns in the new header, compare it to body |
kbrevoort | 970777b | 2019-09-05 23:26:44 -0400 | [diff] [blame] | 138 | kable_xml_tbody <- xml_tpart(kable_xml, 'tbody') |
| 139 | body_rows <- xml_children(kable_xml_tbody) |
| 140 | kable_ncol <- max(xml_length(body_rows)) |
| 141 | } else { |
| 142 | header_rows <- xml_children(kable_xml_thead) |
| 143 | bottom_header_row <- header_rows[[length(header_rows)]] |
| 144 | kable_ncol <- length(xml_children(bottom_header_row)) |
| 145 | } |
| 146 | |
Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 147 | if (sum(header$colspan) != kable_ncol) { |
Hao Zhu | b80888d | 2020-10-18 14:14:30 -0400 | [diff] [blame^] | 148 | stop("The new header row you provided has a total of ", sum(header$colspan), |
| 149 | " columns but the original kable_input has ", kable_ncol, ".") |
Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 150 | } |
| 151 | |
Hao Zhu | 76f0eb6 | 2018-09-15 12:38:33 -0400 | [diff] [blame] | 152 | new_header_row <- htmlTable_new_header_generator( |
Hao Zhu | b548aac | 2018-09-18 13:29:01 -0400 | [diff] [blame] | 153 | header, bold, italic, monospace, underline, strikeout, align, |
Hao Zhu | 8b16a6c | 2020-08-18 16:59:20 -0400 | [diff] [blame] | 154 | color, background, font_size, angle, line, line_sep, extra_css, |
| 155 | include_empty, attr(kable_input, 'lightable_class') |
Hao Zhu | 76f0eb6 | 2018-09-15 12:38:33 -0400 | [diff] [blame] | 156 | ) |
Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 157 | xml_add_child(kable_xml_thead, new_header_row, .where = 0) |
Hao Zhu | f2dfd14 | 2017-07-24 14:43:28 -0400 | [diff] [blame] | 158 | out <- as_kable_xml(kable_xml) |
Hao Zhu | 2345676 | 2018-03-26 12:30:10 -0400 | [diff] [blame] | 159 | if (is.null(kable_attrs$header_above)) { |
| 160 | kable_attrs$header_above <- 1 |
| 161 | } else { |
| 162 | kable_attrs$header_above <- kable_attrs$header_above + 1 |
| 163 | } |
Hao Zhu | 909ea38 | 2017-06-12 15:43:47 -0400 | [diff] [blame] | 164 | attributes(out) <- kable_attrs |
Hao Zhu | f210083 | 2018-01-11 16:20:29 -0500 | [diff] [blame] | 165 | if (!"kableExtra" %in% class(out)) class(out) <- c("kableExtra", class(out)) |
Hao Zhu | 6a07646 | 2017-03-01 12:59:01 -0500 | [diff] [blame] | 166 | return(out) |
Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | standardize_header_input <- function(header) { |
| 170 | header_names <- names(header) |
| 171 | |
| 172 | if (is.null(header_names)) { |
| 173 | return(data.frame(header = header, colspan = 1, row.names = NULL)) |
| 174 | } |
| 175 | |
| 176 | names(header)[header_names == ""] <- header[header_names == ""] |
| 177 | header[header_names == ""] <- 1 |
| 178 | header_names <- names(header) |
| 179 | header <- as.numeric(header) |
| 180 | names(header) <- header_names |
martinschmelzer | f43dcf5 | 2019-01-08 13:02:52 +0100 | [diff] [blame] | 181 | 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] | 182 | } |
| 183 | |
Hao Zhu | 76f0eb6 | 2018-09-15 12:38:33 -0400 | [diff] [blame] | 184 | htmlTable_new_header_generator <- function(header_df, bold, italic, monospace, |
Hao Zhu | b548aac | 2018-09-18 13:29:01 -0400 | [diff] [blame] | 185 | underline, strikeout, align, |
Hao Zhu | 76f0eb6 | 2018-09-15 12:38:33 -0400 | [diff] [blame] | 186 | color, background, font_size, |
Hao Zhu | f64f475 | 2018-10-23 10:35:16 -0400 | [diff] [blame] | 187 | angle, line, line_sep, extra_css, |
Hao Zhu | 8b16a6c | 2020-08-18 16:59:20 -0400 | [diff] [blame] | 188 | include_empty, lightable_class) { |
kbrevoort | c79c5a8 | 2019-08-26 22:44:01 -0400 | [diff] [blame] | 189 | align <- vapply(align, switch_align, 'x', USE.NAMES = FALSE) |
| 190 | |
Hao Zhu | 32f43f7 | 2017-06-20 18:24:54 -0400 | [diff] [blame] | 191 | row_style <- paste0( |
kbrevoort | c79c5a8 | 2019-08-26 22:44:01 -0400 | [diff] [blame] | 192 | "text-align: %s; ", |
Hao Zhu | 32f43f7 | 2017-06-20 18:24:54 -0400 | [diff] [blame] | 193 | ifelse(bold, "font-weight: bold; ", ""), |
Hao Zhu | ac7e70f | 2017-08-02 00:18:36 -0400 | [diff] [blame] | 194 | ifelse(italic, "font-style: italic; ", ""), |
Hao Zhu | 76f0eb6 | 2018-09-15 12:38:33 -0400 | [diff] [blame] | 195 | ifelse(monospace, "font-family: monospace; ", ""), |
| 196 | ifelse(underline, "text-decoration: underline; ", ""), |
| 197 | ifelse(strikeout, "text-decoration: line-through; ", "") |
Hao Zhu | 32f43f7 | 2017-06-20 18:24:54 -0400 | [diff] [blame] | 198 | ) |
Hao Zhu | 76f0eb6 | 2018-09-15 12:38:33 -0400 | [diff] [blame] | 199 | if (!is.null(color)) { |
Hao Zhu | 72917f9 | 2019-03-15 18:41:42 -0400 | [diff] [blame] | 200 | row_style <- paste0(row_style, "color: ", html_color(color), " !important;") |
Hao Zhu | 76f0eb6 | 2018-09-15 12:38:33 -0400 | [diff] [blame] | 201 | } |
| 202 | if (!is.null(background)) { |
| 203 | row_style <- paste0( |
| 204 | row_style, |
| 205 | "padding-right: 4px; padding-left: 4px; ", |
Hao Zhu | 72917f9 | 2019-03-15 18:41:42 -0400 | [diff] [blame] | 206 | "background-color: ", html_color(background), " !important;" |
Hao Zhu | 76f0eb6 | 2018-09-15 12:38:33 -0400 | [diff] [blame] | 207 | ) |
| 208 | } |
| 209 | if (!is.null(font_size)) { |
| 210 | if (is.numeric(font_size)) font_size <- paste0(font_size, "px") |
| 211 | row_style <- paste0(row_style, "font-size: ", font_size, ";") |
| 212 | } |
Hao Zhu | 6d68f7c | 2018-09-15 12:44:15 -0400 | [diff] [blame] | 213 | if (!is.null(extra_css)) { |
| 214 | row_style <- paste0(row_style, extra_css) |
| 215 | } |
| 216 | |
Hao Zhu | 76f0eb6 | 2018-09-15 12:38:33 -0400 | [diff] [blame] | 217 | if (!is.null(angle)) { |
| 218 | angle <- paste0("-webkit-transform: rotate(", angle, |
| 219 | "deg); -moz-transform: rotate(", angle, |
| 220 | "deg); -ms-transform: rotate(", angle, |
| 221 | "deg); -o-transform: rotate(", angle, |
| 222 | "deg); transform: rotate(", angle, |
| 223 | "deg); display: inline-block; ") |
Hao Zhu | f64f475 | 2018-10-23 10:35:16 -0400 | [diff] [blame] | 224 | header_df$header <- ifelse( |
Hao Zhu | ccb812f | 2020-08-18 23:27:04 -0400 | [diff] [blame] | 225 | trimws(header_df$header) == "" | include_empty, |
Hao Zhu | f64f475 | 2018-10-23 10:35:16 -0400 | [diff] [blame] | 226 | header_df$header, |
| 227 | paste0('<span style="', angle, '">', header_df$header, '</span>') |
| 228 | ) |
Hao Zhu | 76f0eb6 | 2018-09-15 12:38:33 -0400 | [diff] [blame] | 229 | } |
| 230 | |
Hao Zhu | 8b16a6c | 2020-08-18 16:59:20 -0400 | [diff] [blame] | 231 | if (is.null(lightable_class)) { |
| 232 | border_hidden <- 'border-bottom:hidden;' |
| 233 | line <- ifelse(ez_rep(line, nrow(header_df)), |
| 234 | "border-bottom: 1px solid #ddd; padding-bottom: 5px; ", "") |
| 235 | } else { |
| 236 | border_hidden <- '' |
| 237 | if (lightable_class %in% c("lightable-classic", "lightable-classic-2")) { |
| 238 | line <- ifelse(ez_rep(line, nrow(header_df)), |
| 239 | "border-bottom: 1px solid #111111; margin-bottom: -1px; ", "") |
| 240 | } |
| 241 | if (lightable_class %in% c("lightable-minimal")) { |
| 242 | line <- ifelse(ez_rep(line, nrow(header_df)), |
| 243 | "border-bottom: 2px solid #00000050; ", "") |
| 244 | } |
| 245 | if (lightable_class %in% c("lightable-paper")) { |
| 246 | line <- ifelse(ez_rep(line, nrow(header_df)), |
| 247 | "border-bottom: 1px solid #00000020; padding-bottom: 5px; ", "") |
| 248 | } |
| 249 | if (lightable_class %in% c("lightable-material")) { |
| 250 | line <- ifelse(ez_rep(line, nrow(header_df)), |
| 251 | "border-bottom: 1px solid #eee; padding-bottom: 16px; padding-top: 16px; height: 56px;", "") |
| 252 | } |
| 253 | if (lightable_class %in% c("lightable-material-dark")) { |
| 254 | line <- ifelse(ez_rep(line, nrow(header_df)), |
| 255 | "border-bottom: 1px solid #FFFFFF12; padding-bottom: 16px; padding-top: 16px; height: 56px;", "") |
| 256 | } |
| 257 | } |
| 258 | |
Hao Zhu | b548aac | 2018-09-18 13:29:01 -0400 | [diff] [blame] | 259 | line_sep <- ez_rep(line_sep, nrow(header_df)) |
| 260 | 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] | 261 | |
kbrevoort | c79c5a8 | 2019-08-26 22:44:01 -0400 | [diff] [blame] | 262 | row_style <- sprintf(row_style, align) |
| 263 | |
Hao Zhu | 76f0eb6 | 2018-09-15 12:38:33 -0400 | [diff] [blame] | 264 | header_items <- ifelse( |
Hao Zhu | ccb812f | 2020-08-18 23:27:04 -0400 | [diff] [blame] | 265 | trimws(header_df$header) == "" | include_empty, |
Hao Zhu | 8b16a6c | 2020-08-18 16:59:20 -0400 | [diff] [blame] | 266 | paste0('<th style="empty-cells: hide;', border_hidden, '" colspan="', header_df$colspan, |
Hao Zhu | 76f0eb6 | 2018-09-15 12:38:33 -0400 | [diff] [blame] | 267 | '"></th>'), |
| 268 | paste0( |
Hao Zhu | 8b16a6c | 2020-08-18 16:59:20 -0400 | [diff] [blame] | 269 | '<th style="', border_hidden, 'padding-bottom:0; ', |
Hao Zhu | b548aac | 2018-09-18 13:29:01 -0400 | [diff] [blame] | 270 | line_sep, row_style, '" colspan="', |
Hao Zhu | 76f0eb6 | 2018-09-15 12:38:33 -0400 | [diff] [blame] | 271 | header_df$colspan, '"><div style="', line, '">', header_df$header, |
| 272 | '</div></th>') |
| 273 | ) |
Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 274 | header_text <- paste(c("<tr>", header_items, "</tr>"), collapse = "") |
| 275 | header_xml <- read_xml(header_text, options = c("COMPACT")) |
| 276 | return(header_xml) |
| 277 | } |
| 278 | |
| 279 | # 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] | 280 | pdfTable_add_header_above <- function(kable_input, header, bold, italic, |
Hao Zhu | b548aac | 2018-09-18 13:29:01 -0400 | [diff] [blame] | 281 | monospace, underline, strikeout, align, |
| 282 | color, background, font_size, angle, |
Hao Zhu | 8b2126e | 2019-04-16 17:58:01 -0400 | [diff] [blame] | 283 | escape, line, line_sep, |
| 284 | border_left, border_right) { |
Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 285 | table_info <- magic_mirror(kable_input) |
Hao Zhu | 7012cd0 | 2020-08-09 15:40:50 -0400 | [diff] [blame] | 286 | |
jokorn | a00ad66 | 2019-11-07 21:11:19 +0100 | [diff] [blame] | 287 | if (is.data.frame(header)){ |
jokorn | 1cb595e | 2019-11-07 21:26:54 +0100 | [diff] [blame] | 288 | if(ncol(header) == 2 & is.character(header[[1]]) & is.numeric(header[[2]])){ |
jokorn | 19f6b53 | 2019-11-07 21:57:53 +0100 | [diff] [blame] | 289 | header <- data.frame(header = header[[1]], colspan = header[[2]], |
| 290 | stringsAsFactors = FALSE) |
jokorn | a00ad66 | 2019-11-07 21:11:19 +0100 | [diff] [blame] | 291 | } |
| 292 | else { |
| 293 | stop("If header input is provided as a data frame instead of a named vector ", |
| 294 | "it must consist of only two columns: ", |
| 295 | "The first should be a character vector with ", |
| 296 | "header names and the second should be a numeric vector with ", |
| 297 | "the number of columns the header should span.") |
| 298 | } |
| 299 | } |
| 300 | else { |
| 301 | header <- standardize_header_input(header) |
| 302 | } |
Hao Zhu | 248bbef | 2018-04-02 18:25:14 -0400 | [diff] [blame] | 303 | |
Hao Zhu | ac7e70f | 2017-08-02 00:18:36 -0400 | [diff] [blame] | 304 | if (escape) { |
Hao Zhu | f94a26f | 2018-04-05 17:42:55 -0400 | [diff] [blame] | 305 | header$header <- input_escape(header$header, align) |
Hao Zhu | ac7e70f | 2017-08-02 00:18:36 -0400 | [diff] [blame] | 306 | } |
Hao Zhu | 248bbef | 2018-04-02 18:25:14 -0400 | [diff] [blame] | 307 | |
kbrevoort | c79c5a8 | 2019-08-26 22:44:01 -0400 | [diff] [blame] | 308 | align <- vapply(align, match.arg, 'a', choices = c("l", "c", "r")) |
Hao Zhu | 248bbef | 2018-04-02 18:25:14 -0400 | [diff] [blame] | 309 | |
Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 310 | hline_type <- switch(table_info$booktabs + 1, "\\\\hline", "\\\\toprule") |
Hao Zhu | 3465b50 | 2018-04-02 22:41:46 -0400 | [diff] [blame] | 311 | new_header_split <- pdfTable_new_header_generator( |
Hao Zhu | 76f0eb6 | 2018-09-15 12:38:33 -0400 | [diff] [blame] | 312 | header, table_info$booktabs, bold, italic, monospace, underline, strikeout, |
Hao Zhu | 8b2126e | 2019-04-16 17:58:01 -0400 | [diff] [blame] | 313 | align, color, background, font_size, angle, line_sep, |
| 314 | border_left, border_right) |
Hao Zhu | 76f0eb6 | 2018-09-15 12:38:33 -0400 | [diff] [blame] | 315 | if (line) { |
Salzer | 6bc84f8 | 2018-02-11 13:18:01 -0500 | [diff] [blame] | 316 | new_header <- paste0(new_header_split[1], "\n", new_header_split[2]) |
| 317 | } else { |
| 318 | new_header <- new_header_split[1] |
| 319 | } |
Hao Zhu | 3fc0e88 | 2018-04-03 16:06:41 -0400 | [diff] [blame] | 320 | out <- str_replace(solve_enc(kable_input), |
Hao Zhu | b2b4199 | 2017-10-03 12:50:03 -0400 | [diff] [blame] | 321 | hline_type, |
| 322 | paste0(hline_type, "\n", new_header)) |
Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 323 | out <- structure(out, format = "latex", class = "knitr_kable") |
Hao Zhu | 2ce42b9 | 2017-06-15 17:15:33 -0400 | [diff] [blame] | 324 | # new_header_row <- latex_contents_escape(new_header_split[1]) |
| 325 | if (is.null(table_info$new_header_row)) { |
| 326 | table_info$new_header_row <- new_header_split[1] |
| 327 | table_info$header_df <- list(header) |
| 328 | } else { |
| 329 | table_info$new_header_row <- c(table_info$new_header_row, new_header_split[1]) |
| 330 | table_info$header_df[[length(table_info$header_df) + 1]] <- header |
| 331 | } |
Hao Zhu | 32f43f7 | 2017-06-20 18:24:54 -0400 | [diff] [blame] | 332 | attr(out, "kable_meta") <- table_info |
Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 333 | return(out) |
| 334 | } |
| 335 | |
Hao Zhu | 76f0eb6 | 2018-09-15 12:38:33 -0400 | [diff] [blame] | 336 | ez_rep <- function(x, n) { |
| 337 | if (is.null(x)) return(NULL) |
| 338 | if (length(x) == 1) return(rep(x, n)) |
| 339 | return(x) |
| 340 | } |
| 341 | |
Hao Zhu | 32f43f7 | 2017-06-20 18:24:54 -0400 | [diff] [blame] | 342 | pdfTable_new_header_generator <- function(header_df, booktabs = FALSE, |
Hao Zhu | 76f0eb6 | 2018-09-15 12:38:33 -0400 | [diff] [blame] | 343 | bold, italic, monospace, |
| 344 | underline, strikeout, align, |
Hao Zhu | b548aac | 2018-09-18 13:29:01 -0400 | [diff] [blame] | 345 | color, background, font_size, angle, |
Hao Zhu | 8b2126e | 2019-04-16 17:58:01 -0400 | [diff] [blame] | 346 | line_sep, border_left, border_right) { |
Hao Zhu | 76f0eb6 | 2018-09-15 12:38:33 -0400 | [diff] [blame] | 347 | n <- nrow(header_df) |
| 348 | bold <- ez_rep(bold, n) |
| 349 | italic <- ez_rep(italic, n) |
| 350 | monospace <- ez_rep(monospace, n) |
| 351 | underline <- ez_rep(underline, n) |
| 352 | strikeout <- ez_rep(strikeout, n) |
| 353 | align <- ez_rep(align, n) |
| 354 | color <- ez_rep(color, n) |
| 355 | background <- ez_rep(background, n) |
| 356 | font_size <- ez_rep(font_size, n) |
| 357 | angle <- ez_rep(angle, n) |
Hao Zhu | 8b2126e | 2019-04-16 17:58:01 -0400 | [diff] [blame] | 358 | if (!booktabs & n != 1) { |
martinschmelzer | f43dcf5 | 2019-01-08 13:02:52 +0100 | [diff] [blame] | 359 | align[1:(n - 1)] <- paste0(align[1:(n - 1)], "|") |
Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 360 | } |
Hao Zhu | 8b2126e | 2019-04-16 17:58:01 -0400 | [diff] [blame] | 361 | if (border_left) { |
| 362 | align[1] <- paste0("|", align[1]) |
| 363 | } |
| 364 | if (border_right) { |
| 365 | align[n] <- paste0(align[n], "|") |
| 366 | } |
| 367 | |
Hao Zhu | 76f0eb6 | 2018-09-15 12:38:33 -0400 | [diff] [blame] | 368 | header <- header_df$header |
| 369 | colspan <- header_df$colspan |
| 370 | |
| 371 | header <- ifelse(bold, paste0('\\\\textbf\\{', header, '\\}'), header) |
| 372 | header <- ifelse(italic, paste0('\\\\em\\{', header, '\\}'), header) |
| 373 | header <- ifelse(monospace, paste0('\\\\ttfamily\\{', header, '\\}'), header) |
| 374 | header <- ifelse(underline, paste0('\\\\underline\\{', header, '\\}'), header) |
| 375 | header <- ifelse(strikeout, paste0('\\\\sout\\{', header, '\\}'), header) |
| 376 | if (!is.null(color)) { |
| 377 | color <- latex_color(color) |
| 378 | header <- paste0("\\\\textcolor", color, "\\{", header, "\\}") |
| 379 | } |
| 380 | if (!is.null(background)) { |
| 381 | background <- latex_color(background) |
| 382 | header <- paste0("\\\\cellcolor", background, "\\{", header, "\\}") |
| 383 | } |
| 384 | if (!is.null(font_size)) { |
| 385 | header <- paste0("\\\\bgroup\\\\fontsize\\{", font_size, "\\}\\{", |
| 386 | as.numeric(font_size) + 2, |
| 387 | "\\}\\\\selectfont ", header, "\\\\egroup\\{\\}") |
| 388 | } |
| 389 | if (!is.null(angle)) { |
| 390 | header <- paste0("\\\\rotatebox\\{", angle, "\\}\\{", header, "\\}") |
| 391 | } |
| 392 | header_items <- paste0( |
| 393 | '\\\\multicolumn\\{', colspan, '\\}\\{', align, '\\}\\{', header, '\\}' |
| 394 | ) |
| 395 | |
Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 396 | header_text <- paste(paste(header_items, collapse = " & "), "\\\\\\\\") |
Hao Zhu | b548aac | 2018-09-18 13:29:01 -0400 | [diff] [blame] | 397 | cline <- cline_gen(header_df, booktabs, line_sep) |
Hao Zhu | 2ce42b9 | 2017-06-15 17:15:33 -0400 | [diff] [blame] | 398 | return(c(header_text, cline)) |
| 399 | } |
| 400 | |
Hao Zhu | b548aac | 2018-09-18 13:29:01 -0400 | [diff] [blame] | 401 | cline_gen <- function(header_df, booktabs, line_sep) { |
Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 402 | cline_end <- cumsum(header_df$colspan) |
| 403 | cline_start <- c(0, cline_end) + 1 |
| 404 | cline_start <- cline_start[-length(cline_start)] |
Hao Zhu | b548aac | 2018-09-18 13:29:01 -0400 | [diff] [blame] | 405 | cline_type <- switch( |
| 406 | booktabs + 1, |
| 407 | "\\\\cline{", |
| 408 | glue::glue("\\\\cmidrule(l{[line_sep]pt}r{[line_sep]pt}){", |
| 409 | .open = "[", .close = "]")) |
Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 410 | cline <- paste0(cline_type, cline_start, "-", cline_end, "}") |
| 411 | cline <- cline[trimws(header_df$header) != ""] |
| 412 | cline <- paste(cline, collapse = " ") |
Hao Zhu | 2ce42b9 | 2017-06-15 17:15:33 -0400 | [diff] [blame] | 413 | return(cline) |
Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 414 | } |
Hao Zhu | 2ce42b9 | 2017-06-15 17:15:33 -0400 | [diff] [blame] | 415 | |
kbrevoort | c79c5a8 | 2019-08-26 22:44:01 -0400 | [diff] [blame] | 416 | switch_align <- function(x) { |
| 417 | if (x %in% c('l', 'c', 'r')) { |
| 418 | return(switch(x, l = 'left', c = 'center', r = 'right')) |
| 419 | } |
| 420 | return(x) |
| 421 | } |
Hao Zhu | 2ce42b9 | 2017-06-15 17:15:33 -0400 | [diff] [blame] | 422 | |