blob: 48fbc3f502d111663d6ce2cc5874454df715c341 [file] [log] [blame]
Hao Zhuf7994dd2017-02-27 16:58:42 -05001#' 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 Zhue7c8f702017-10-10 13:22:59 -04005#' function and adds an header row on top of it.
Hao Zhuf7994dd2017-02-27 16:58:42 -05006#'
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)`.
jokorna00ad662019-11-07 21:11:19 +010013#' Alternatively, a data frame with two columns can be provided: The first
Hao Zhu7012cd02020-08-09 15:40:50 -040014#' column should contain the header names (character vector) and the second
jokorna00ad662019-11-07 21:11:19 +010015#' column should contain the colspan (numeric vector). This input can be used
16#' if there are problems with unicode characters in the headers.
Hao Zhu32f43f72017-06-20 18:24:54 -040017#' @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 Zhuac7e70f2017-08-02 00:18:36 -040019#' @param monospace A T/F value to control whether the text of the selected column
20#' need to be monospaced (verbatim)
Hao Zhu76f0eb62018-09-15 12:38:33 -040021#' @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 Zhu3465b502018-04-02 22:41:46 -040025#' @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 Zhu76f0eb62018-09-15 12:38:33 -040028#' @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 Zhuac7e70f2017-08-02 00:18:36 -040038#' @param escape A T/F value showing whether special characters should be
39#' escaped.
Salzer6bc84f82018-02-11 13:18:01 -050040#' @param line A T/F value to control whether a line will appear underneath the
41#' header
Hao Zhub548aac2018-09-18 13:29:01 -040042#' @param line_sep A numeric value indicating how much the midlines should be
43#' separated by space. Default is 3.
Hao Zhu6d68f7c2018-09-15 12:44:15 -040044#' @param extra_css An HTML only option. CSS defined here will be send to the
45#' td cell.
Hao Zhuf64f4752018-10-23 10:35:16 -040046#' @param include_empty Whether empty cells in HTML should also be styled.
47#' Default is FALSE.
Hao Zhu8b2126e2019-04-16 17:58:01 -040048#' @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 Zhuf7994dd2017-02-27 16:58:42 -050050#'
Hao Zhu78e61222017-05-24 20:53:35 -040051#' @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 Zhuc1f38412017-02-23 12:13:48 -050056#' @export
Hao Zhuac7e70f2017-08-02 00:18:36 -040057add_header_above <- function(kable_input, header = NULL,
Hao Zhu76f0eb62018-09-15 12:38:33 -040058 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 Zhub548aac2018-09-18 13:29:01 -040062 escape = TRUE, line = TRUE, line_sep = 3,
Hao Zhu8b2126e2019-04-16 17:58:01 -040063 extra_css = NULL, include_empty = FALSE,
64 border_left = FALSE, border_right = FALSE) {
kbrevoortcd16bc52019-09-03 21:25:33 -040065 if (is.null(header)) return(kable_input)
66
Hao Zhuc1f38412017-02-23 12:13:48 -050067 kable_format <- attr(kable_input, "format")
68 if (!kable_format %in% c("html", "latex")) {
Hao Zhu401ebd82018-01-14 17:10:20 -050069 warning("Please specify format in kable. kableExtra can customize either ",
Hao Zhu76f0eb62018-09-15 12:38:33 -040070 "HTML or LaTeX outputs. See https://haozhu233.github.io/kableExtra/ ",
71 "for details.")
Hao Zhu401ebd82018-01-14 17:10:20 -050072 return(kable_input)
Hao Zhuc1f38412017-02-23 12:13:48 -050073 }
kbrevoortc79c5a82019-08-26 22:44:01 -040074 if ((length(align) != 1L) & (length(align) != length(header))) {
75 warning("Length of align vector supplied to add_header_above must either be 1 ",
76 "or the same length as the header supplied. The length of the align ",
77 sprintf("vector supplied is %i and the header length is %i.",
78 length(align), length(header)),
79 "Using default of centering each element of row.")
80 align <- 'c'
81 }
Hao Zhu7012cd02020-08-09 15:40:50 -040082 if (is.null(header)) return(kable_input)
83 if (is.data.frame(header)){
84 if(ncol(header) == 2 & is.character(header[[1]]) & is.numeric(header[[2]])){
85 header <- data.frame(header = header[[1]], colspan = header[[2]],
86 stringsAsFactors = FALSE)
87 }
88 else {
89 stop("If header input is provided as a data frame instead of a named",
90 "vector it must consist of only two columns: ",
91 "The first should be a character vector with ",
92 "header names and the second should be a numeric vector with ",
93 "the number of columns the header should span.")
94 }
95 }
96 else {
97 header <- standardize_header_input(header)
98 }
Hao Zhuc1f38412017-02-23 12:13:48 -050099 if (kable_format == "html") {
Hao Zhuf64f4752018-10-23 10:35:16 -0400100 return(htmlTable_add_header_above(
101 kable_input, header, bold, italic, monospace, underline, strikeout,
102 align, color, background, font_size, angle, escape, line, line_sep,
103 extra_css, include_empty
104 ))
Hao Zhuc1f38412017-02-23 12:13:48 -0500105 }
106 if (kable_format == "latex") {
Hao Zhuf64f4752018-10-23 10:35:16 -0400107 return(pdfTable_add_header_above(
108 kable_input, header, bold, italic, monospace, underline, strikeout,
Hao Zhu8b2126e2019-04-16 17:58:01 -0400109 align, color, background, font_size, angle, escape, line, line_sep,
110 border_left, border_right))
Hao Zhuc1f38412017-02-23 12:13:48 -0500111 }
112}
113
114# HTML
Hao Zhu3465b502018-04-02 22:41:46 -0400115htmlTable_add_header_above <- function(kable_input, header, bold, italic,
Hao Zhu76f0eb62018-09-15 12:38:33 -0400116 monospace, underline, strikeout,
Hao Zhu6d68f7c2018-09-15 12:44:15 -0400117 align, color, background, font_size,
Hao Zhub548aac2018-09-18 13:29:01 -0400118 angle, escape, line, line_sep,
Hao Zhuf64f4752018-10-23 10:35:16 -0400119 extra_css, include_empty) {
Hao Zhu909ea382017-06-12 15:43:47 -0400120 kable_attrs <- attributes(kable_input)
Hao Zhu558c72f2017-07-24 15:12:00 -0400121 kable_xml <- read_kable_as_xml(kable_input)
Hao Zhu62cdde52017-05-20 22:16:03 -0400122 kable_xml_thead <- xml_tpart(kable_xml, "thead")
Hao Zhuc1f38412017-02-23 12:13:48 -0500123
Hao Zhuac7e70f2017-08-02 00:18:36 -0400124 if (escape) {
125 header$header <- escape_html(header$header)
126 }
127
kbrevoort970777b2019-09-05 23:26:44 -0400128 # If there is no existing header, add one
129 if (is.null(kable_xml_thead)) {
Hao Zhu69fa19d2020-08-12 08:31:14 -0400130 # Add thead node as first child
131 xml_add_child(kable_xml, 'thead', .where = 0)
kbrevoort970777b2019-09-05 23:26:44 -0400132 kable_xml_thead <- xml_tpart(kable_xml, 'thead')
133
Hao Zhu69fa19d2020-08-12 08:31:14 -0400134 # To check the number of columns in the new header, compare it to body
kbrevoort970777b2019-09-05 23:26:44 -0400135 kable_xml_tbody <- xml_tpart(kable_xml, 'tbody')
136 body_rows <- xml_children(kable_xml_tbody)
137 kable_ncol <- max(xml_length(body_rows))
138 } else {
139 header_rows <- xml_children(kable_xml_thead)
140 bottom_header_row <- header_rows[[length(header_rows)]]
141 kable_ncol <- length(xml_children(bottom_header_row))
142 }
143
Hao Zhuc1f38412017-02-23 12:13:48 -0500144 if (sum(header$colspan) != kable_ncol) {
145 stop("The new header row you provided has a different total number of ",
146 "columns with the original kable output.")
147 }
148
Hao Zhu76f0eb62018-09-15 12:38:33 -0400149 new_header_row <- htmlTable_new_header_generator(
Hao Zhub548aac2018-09-18 13:29:01 -0400150 header, bold, italic, monospace, underline, strikeout, align,
Hao Zhu8b16a6c2020-08-18 16:59:20 -0400151 color, background, font_size, angle, line, line_sep, extra_css,
152 include_empty, attr(kable_input, 'lightable_class')
Hao Zhu76f0eb62018-09-15 12:38:33 -0400153 )
Hao Zhuc1f38412017-02-23 12:13:48 -0500154 xml_add_child(kable_xml_thead, new_header_row, .where = 0)
Hao Zhuf2dfd142017-07-24 14:43:28 -0400155 out <- as_kable_xml(kable_xml)
Hao Zhu23456762018-03-26 12:30:10 -0400156 if (is.null(kable_attrs$header_above)) {
157 kable_attrs$header_above <- 1
158 } else {
159 kable_attrs$header_above <- kable_attrs$header_above + 1
160 }
Hao Zhu909ea382017-06-12 15:43:47 -0400161 attributes(out) <- kable_attrs
Hao Zhuf2100832018-01-11 16:20:29 -0500162 if (!"kableExtra" %in% class(out)) class(out) <- c("kableExtra", class(out))
Hao Zhu6a076462017-03-01 12:59:01 -0500163 return(out)
Hao Zhuc1f38412017-02-23 12:13:48 -0500164}
165
166standardize_header_input <- function(header) {
167 header_names <- names(header)
168
169 if (is.null(header_names)) {
170 return(data.frame(header = header, colspan = 1, row.names = NULL))
171 }
172
173 names(header)[header_names == ""] <- header[header_names == ""]
174 header[header_names == ""] <- 1
175 header_names <- names(header)
176 header <- as.numeric(header)
177 names(header) <- header_names
martinschmelzerf43dcf52019-01-08 13:02:52 +0100178 return(data.frame(header = names(header), colspan = header, row.names = NULL, stringsAsFactors = F))
Hao Zhuc1f38412017-02-23 12:13:48 -0500179}
180
Hao Zhu76f0eb62018-09-15 12:38:33 -0400181htmlTable_new_header_generator <- function(header_df, bold, italic, monospace,
Hao Zhub548aac2018-09-18 13:29:01 -0400182 underline, strikeout, align,
Hao Zhu76f0eb62018-09-15 12:38:33 -0400183 color, background, font_size,
Hao Zhuf64f4752018-10-23 10:35:16 -0400184 angle, line, line_sep, extra_css,
Hao Zhu8b16a6c2020-08-18 16:59:20 -0400185 include_empty, lightable_class) {
kbrevoortc79c5a82019-08-26 22:44:01 -0400186 align <- vapply(align, switch_align, 'x', USE.NAMES = FALSE)
187
Hao Zhu32f43f72017-06-20 18:24:54 -0400188 row_style <- paste0(
kbrevoortc79c5a82019-08-26 22:44:01 -0400189 "text-align: %s; ",
Hao Zhu32f43f72017-06-20 18:24:54 -0400190 ifelse(bold, "font-weight: bold; ", ""),
Hao Zhuac7e70f2017-08-02 00:18:36 -0400191 ifelse(italic, "font-style: italic; ", ""),
Hao Zhu76f0eb62018-09-15 12:38:33 -0400192 ifelse(monospace, "font-family: monospace; ", ""),
193 ifelse(underline, "text-decoration: underline; ", ""),
194 ifelse(strikeout, "text-decoration: line-through; ", "")
Hao Zhu32f43f72017-06-20 18:24:54 -0400195 )
Hao Zhu76f0eb62018-09-15 12:38:33 -0400196 if (!is.null(color)) {
Hao Zhu72917f92019-03-15 18:41:42 -0400197 row_style <- paste0(row_style, "color: ", html_color(color), " !important;")
Hao Zhu76f0eb62018-09-15 12:38:33 -0400198 }
199 if (!is.null(background)) {
200 row_style <- paste0(
201 row_style,
202 "padding-right: 4px; padding-left: 4px; ",
Hao Zhu72917f92019-03-15 18:41:42 -0400203 "background-color: ", html_color(background), " !important;"
Hao Zhu76f0eb62018-09-15 12:38:33 -0400204 )
205 }
206 if (!is.null(font_size)) {
207 if (is.numeric(font_size)) font_size <- paste0(font_size, "px")
208 row_style <- paste0(row_style, "font-size: ", font_size, ";")
209 }
Hao Zhu6d68f7c2018-09-15 12:44:15 -0400210 if (!is.null(extra_css)) {
211 row_style <- paste0(row_style, extra_css)
212 }
213
Hao Zhu76f0eb62018-09-15 12:38:33 -0400214 if (!is.null(angle)) {
215 angle <- paste0("-webkit-transform: rotate(", angle,
216 "deg); -moz-transform: rotate(", angle,
217 "deg); -ms-transform: rotate(", angle,
218 "deg); -o-transform: rotate(", angle,
219 "deg); transform: rotate(", angle,
220 "deg); display: inline-block; ")
Hao Zhuf64f4752018-10-23 10:35:16 -0400221 header_df$header <- ifelse(
Hao Zhuccb812f2020-08-18 23:27:04 -0400222 trimws(header_df$header) == "" | include_empty,
Hao Zhuf64f4752018-10-23 10:35:16 -0400223 header_df$header,
224 paste0('<span style="', angle, '">', header_df$header, '</span>')
225 )
Hao Zhu76f0eb62018-09-15 12:38:33 -0400226 }
227
Hao Zhu8b16a6c2020-08-18 16:59:20 -0400228 if (is.null(lightable_class)) {
229 border_hidden <- 'border-bottom:hidden;'
230 line <- ifelse(ez_rep(line, nrow(header_df)),
231 "border-bottom: 1px solid #ddd; padding-bottom: 5px; ", "")
232 } else {
233 border_hidden <- ''
234 if (lightable_class %in% c("lightable-classic", "lightable-classic-2")) {
235 line <- ifelse(ez_rep(line, nrow(header_df)),
236 "border-bottom: 1px solid #111111; margin-bottom: -1px; ", "")
237 }
238 if (lightable_class %in% c("lightable-minimal")) {
239 line <- ifelse(ez_rep(line, nrow(header_df)),
240 "border-bottom: 2px solid #00000050; ", "")
241 }
242 if (lightable_class %in% c("lightable-paper")) {
243 line <- ifelse(ez_rep(line, nrow(header_df)),
244 "border-bottom: 1px solid #00000020; padding-bottom: 5px; ", "")
245 }
246 if (lightable_class %in% c("lightable-material")) {
247 line <- ifelse(ez_rep(line, nrow(header_df)),
248 "border-bottom: 1px solid #eee; padding-bottom: 16px; padding-top: 16px; height: 56px;", "")
249 }
250 if (lightable_class %in% c("lightable-material-dark")) {
251 line <- ifelse(ez_rep(line, nrow(header_df)),
252 "border-bottom: 1px solid #FFFFFF12; padding-bottom: 16px; padding-top: 16px; height: 56px;", "")
253 }
254 }
255
Hao Zhub548aac2018-09-18 13:29:01 -0400256 line_sep <- ez_rep(line_sep, nrow(header_df))
257 line_sep <- glue::glue('padding-left:{line_sep}px;padding-right:{line_sep}px;')
Hao Zhu76f0eb62018-09-15 12:38:33 -0400258
kbrevoortc79c5a82019-08-26 22:44:01 -0400259 row_style <- sprintf(row_style, align)
260
Hao Zhu76f0eb62018-09-15 12:38:33 -0400261 header_items <- ifelse(
Hao Zhuccb812f2020-08-18 23:27:04 -0400262 trimws(header_df$header) == "" | include_empty,
Hao Zhu8b16a6c2020-08-18 16:59:20 -0400263 paste0('<th style="empty-cells: hide;', border_hidden, '" colspan="', header_df$colspan,
Hao Zhu76f0eb62018-09-15 12:38:33 -0400264 '"></th>'),
265 paste0(
Hao Zhu8b16a6c2020-08-18 16:59:20 -0400266 '<th style="', border_hidden, 'padding-bottom:0; ',
Hao Zhub548aac2018-09-18 13:29:01 -0400267 line_sep, row_style, '" colspan="',
Hao Zhu76f0eb62018-09-15 12:38:33 -0400268 header_df$colspan, '"><div style="', line, '">', header_df$header,
269 '</div></th>')
270 )
Hao Zhuc1f38412017-02-23 12:13:48 -0500271 header_text <- paste(c("<tr>", header_items, "</tr>"), collapse = "")
272 header_xml <- read_xml(header_text, options = c("COMPACT"))
273 return(header_xml)
274}
275
276# Add an extra header row above the current header in a LaTeX table ------
Hao Zhu3465b502018-04-02 22:41:46 -0400277pdfTable_add_header_above <- function(kable_input, header, bold, italic,
Hao Zhub548aac2018-09-18 13:29:01 -0400278 monospace, underline, strikeout, align,
279 color, background, font_size, angle,
Hao Zhu8b2126e2019-04-16 17:58:01 -0400280 escape, line, line_sep,
281 border_left, border_right) {
Hao Zhuc1f38412017-02-23 12:13:48 -0500282 table_info <- magic_mirror(kable_input)
Hao Zhu7012cd02020-08-09 15:40:50 -0400283
jokorna00ad662019-11-07 21:11:19 +0100284 if (is.data.frame(header)){
jokorn1cb595e2019-11-07 21:26:54 +0100285 if(ncol(header) == 2 & is.character(header[[1]]) & is.numeric(header[[2]])){
jokorn19f6b532019-11-07 21:57:53 +0100286 header <- data.frame(header = header[[1]], colspan = header[[2]],
287 stringsAsFactors = FALSE)
jokorna00ad662019-11-07 21:11:19 +0100288 }
289 else {
290 stop("If header input is provided as a data frame instead of a named vector ",
291 "it must consist of only two columns: ",
292 "The first should be a character vector with ",
293 "header names and the second should be a numeric vector with ",
294 "the number of columns the header should span.")
295 }
296 }
297 else {
298 header <- standardize_header_input(header)
299 }
Hao Zhu248bbef2018-04-02 18:25:14 -0400300
Hao Zhuac7e70f2017-08-02 00:18:36 -0400301 if (escape) {
Hao Zhuf94a26f2018-04-05 17:42:55 -0400302 header$header <- input_escape(header$header, align)
Hao Zhuac7e70f2017-08-02 00:18:36 -0400303 }
Hao Zhu248bbef2018-04-02 18:25:14 -0400304
kbrevoortc79c5a82019-08-26 22:44:01 -0400305 align <- vapply(align, match.arg, 'a', choices = c("l", "c", "r"))
Hao Zhu248bbef2018-04-02 18:25:14 -0400306
Hao Zhuc1f38412017-02-23 12:13:48 -0500307 hline_type <- switch(table_info$booktabs + 1, "\\\\hline", "\\\\toprule")
Hao Zhu3465b502018-04-02 22:41:46 -0400308 new_header_split <- pdfTable_new_header_generator(
Hao Zhu76f0eb62018-09-15 12:38:33 -0400309 header, table_info$booktabs, bold, italic, monospace, underline, strikeout,
Hao Zhu8b2126e2019-04-16 17:58:01 -0400310 align, color, background, font_size, angle, line_sep,
311 border_left, border_right)
Hao Zhu76f0eb62018-09-15 12:38:33 -0400312 if (line) {
Salzer6bc84f82018-02-11 13:18:01 -0500313 new_header <- paste0(new_header_split[1], "\n", new_header_split[2])
314 } else {
315 new_header <- new_header_split[1]
316 }
Hao Zhu3fc0e882018-04-03 16:06:41 -0400317 out <- str_replace(solve_enc(kable_input),
Hao Zhub2b41992017-10-03 12:50:03 -0400318 hline_type,
319 paste0(hline_type, "\n", new_header))
Hao Zhuc1f38412017-02-23 12:13:48 -0500320 out <- structure(out, format = "latex", class = "knitr_kable")
Hao Zhu2ce42b92017-06-15 17:15:33 -0400321 # new_header_row <- latex_contents_escape(new_header_split[1])
322 if (is.null(table_info$new_header_row)) {
323 table_info$new_header_row <- new_header_split[1]
324 table_info$header_df <- list(header)
325 } else {
326 table_info$new_header_row <- c(table_info$new_header_row, new_header_split[1])
327 table_info$header_df[[length(table_info$header_df) + 1]] <- header
328 }
Hao Zhu32f43f72017-06-20 18:24:54 -0400329 attr(out, "kable_meta") <- table_info
Hao Zhuc1f38412017-02-23 12:13:48 -0500330 return(out)
331}
332
Hao Zhu76f0eb62018-09-15 12:38:33 -0400333ez_rep <- function(x, n) {
334 if (is.null(x)) return(NULL)
335 if (length(x) == 1) return(rep(x, n))
336 return(x)
337}
338
Hao Zhu32f43f72017-06-20 18:24:54 -0400339pdfTable_new_header_generator <- function(header_df, booktabs = FALSE,
Hao Zhu76f0eb62018-09-15 12:38:33 -0400340 bold, italic, monospace,
341 underline, strikeout, align,
Hao Zhub548aac2018-09-18 13:29:01 -0400342 color, background, font_size, angle,
Hao Zhu8b2126e2019-04-16 17:58:01 -0400343 line_sep, border_left, border_right) {
Hao Zhu76f0eb62018-09-15 12:38:33 -0400344 n <- nrow(header_df)
345 bold <- ez_rep(bold, n)
346 italic <- ez_rep(italic, n)
347 monospace <- ez_rep(monospace, n)
348 underline <- ez_rep(underline, n)
349 strikeout <- ez_rep(strikeout, n)
350 align <- ez_rep(align, n)
351 color <- ez_rep(color, n)
352 background <- ez_rep(background, n)
353 font_size <- ez_rep(font_size, n)
354 angle <- ez_rep(angle, n)
Hao Zhu8b2126e2019-04-16 17:58:01 -0400355 if (!booktabs & n != 1) {
martinschmelzerf43dcf52019-01-08 13:02:52 +0100356 align[1:(n - 1)] <- paste0(align[1:(n - 1)], "|")
Hao Zhuc1f38412017-02-23 12:13:48 -0500357 }
Hao Zhu8b2126e2019-04-16 17:58:01 -0400358 if (border_left) {
359 align[1] <- paste0("|", align[1])
360 }
361 if (border_right) {
362 align[n] <- paste0(align[n], "|")
363 }
364
Hao Zhu76f0eb62018-09-15 12:38:33 -0400365 header <- header_df$header
366 colspan <- header_df$colspan
367
368 header <- ifelse(bold, paste0('\\\\textbf\\{', header, '\\}'), header)
369 header <- ifelse(italic, paste0('\\\\em\\{', header, '\\}'), header)
370 header <- ifelse(monospace, paste0('\\\\ttfamily\\{', header, '\\}'), header)
371 header <- ifelse(underline, paste0('\\\\underline\\{', header, '\\}'), header)
372 header <- ifelse(strikeout, paste0('\\\\sout\\{', header, '\\}'), header)
373 if (!is.null(color)) {
374 color <- latex_color(color)
375 header <- paste0("\\\\textcolor", color, "\\{", header, "\\}")
376 }
377 if (!is.null(background)) {
378 background <- latex_color(background)
379 header <- paste0("\\\\cellcolor", background, "\\{", header, "\\}")
380 }
381 if (!is.null(font_size)) {
382 header <- paste0("\\\\bgroup\\\\fontsize\\{", font_size, "\\}\\{",
383 as.numeric(font_size) + 2,
384 "\\}\\\\selectfont ", header, "\\\\egroup\\{\\}")
385 }
386 if (!is.null(angle)) {
387 header <- paste0("\\\\rotatebox\\{", angle, "\\}\\{", header, "\\}")
388 }
389 header_items <- paste0(
390 '\\\\multicolumn\\{', colspan, '\\}\\{', align, '\\}\\{', header, '\\}'
391 )
392
Hao Zhuc1f38412017-02-23 12:13:48 -0500393 header_text <- paste(paste(header_items, collapse = " & "), "\\\\\\\\")
Hao Zhub548aac2018-09-18 13:29:01 -0400394 cline <- cline_gen(header_df, booktabs, line_sep)
Hao Zhu2ce42b92017-06-15 17:15:33 -0400395 return(c(header_text, cline))
396}
397
Hao Zhub548aac2018-09-18 13:29:01 -0400398cline_gen <- function(header_df, booktabs, line_sep) {
Hao Zhuc1f38412017-02-23 12:13:48 -0500399 cline_end <- cumsum(header_df$colspan)
400 cline_start <- c(0, cline_end) + 1
401 cline_start <- cline_start[-length(cline_start)]
Hao Zhub548aac2018-09-18 13:29:01 -0400402 cline_type <- switch(
403 booktabs + 1,
404 "\\\\cline{",
405 glue::glue("\\\\cmidrule(l{[line_sep]pt}r{[line_sep]pt}){",
406 .open = "[", .close = "]"))
Hao Zhuc1f38412017-02-23 12:13:48 -0500407 cline <- paste0(cline_type, cline_start, "-", cline_end, "}")
408 cline <- cline[trimws(header_df$header) != ""]
409 cline <- paste(cline, collapse = " ")
Hao Zhu2ce42b92017-06-15 17:15:33 -0400410 return(cline)
Hao Zhuc1f38412017-02-23 12:13:48 -0500411}
Hao Zhu2ce42b92017-06-15 17:15:33 -0400412
kbrevoortc79c5a82019-08-26 22:44:01 -0400413switch_align <- function(x) {
414 if (x %in% c('l', 'c', 'r')) {
415 return(switch(x, l = 'left', c = 'center', r = 'right'))
416 }
417 return(x)
418}
Hao Zhu2ce42b92017-06-15 17:15:33 -0400419