From e8db016e0935d07017fa3222ddaf4926a9c277c1 Mon Sep 17 00:00:00 2001 From: Duncan Murdoch Date: Thu, 5 Dec 2024 09:54:43 -0500 Subject: [PATCH] Allow `line` to be a vector in `add_header_above` for LaTeX as well as HTML. Fixes #700. --- DESCRIPTION | 2 +- R/add_header_above.R | 21 ++++++++++++--------- inst/NEWS.md | 4 +++- man/add_header_above.Rd | 4 ++-- 4 files changed, 18 insertions(+), 13 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index c96cbb9..efe88dc 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: kableExtra Type: Package Title: Construct Complex Table with 'kable' and Pipe Syntax -Version: 1.4.0.8 +Version: 1.4.0.9 Authors@R: c( person('Hao', 'Zhu', email = 'haozhu233@gmail.com', role = c('aut', 'cre'), comment = c(ORCID = '0000-0002-3386-6076')), diff --git a/R/add_header_above.R b/R/add_header_above.R index 8836335..c5812ad 100644 --- a/R/add_header_above.R +++ b/R/add_header_above.R @@ -37,7 +37,7 @@ #' @param angle 0-360, degree that the text will rotate. #' @param escape A T/F value showing whether special characters should be #' escaped. -#' @param line A T/F value to control whether a line will appear underneath the +#' @param line A T/F value/vector to control whether a line will appear underneath the #' header #' @param line_sep A numeric value indicating how much the midlines should be #' separated by space. Default is 3. @@ -50,7 +50,7 @@ #' #' @examples #' \dontrun{ -#' x <- knitr::kable(head(mtcars), "html") +#' x <- kbl(head(mtcars), "html") #' # Add a row of header with 3 columns on the top of the table. The column #' # span for the 2nd and 3rd one are 5 & 6. #' add_header_above(x, c(" ", "Group 1" = 5, "Group 2" = 6)) @@ -327,8 +327,8 @@ pdfTable_add_header_above <- function(kable_input, header, bold, italic, new_header_split <- pdfTable_new_header_generator( header, table_info$booktabs, bold, italic, monospace, underline, strikeout, align, color, background, font_size, angle, line_sep, - border_left, border_right) - if (line) { + border_left, border_right, line) + if (any(line)) { new_header <- paste0(new_header_split[1], "\n", new_header_split[2]) } else { new_header <- new_header_split[1] @@ -359,7 +359,7 @@ pdfTable_new_header_generator <- function(header_df, booktabs = FALSE, bold, italic, monospace, underline, strikeout, align, color, background, font_size, angle, - line_sep, border_left, border_right) { + line_sep, border_left, border_right, line) { n <- nrow(header_df) bold <- ez_rep(bold, n) italic <- ez_rep(italic, n) @@ -410,11 +410,11 @@ pdfTable_new_header_generator <- function(header_df, booktabs = FALSE, ) header_text <- paste(paste(header_items, collapse = " & "), "\\\\\\\\") - cline <- cline_gen(header_df, booktabs, line_sep) + cline <- cline_gen(header_df, booktabs, line_sep, line) return(c(header_text, cline)) } -cline_gen <- function(header_df, booktabs, line_sep) { +cline_gen <- function(header_df, booktabs, line_sep, line) { cline_end <- cumsum(header_df$colspan) cline_start <- c(0, cline_end) + 1 cline_start <- cline_start[-length(cline_start)] @@ -423,8 +423,11 @@ cline_gen <- function(header_df, booktabs, line_sep) { "\\\\cline{", paste0("\\\\cmidrule(l{", line_sep, "pt}r{", line_sep, "pt}){")) cline <- paste0(cline_type, cline_start, "-", cline_end, "}") - cline <- cline[trimws(header_df$header) != ""] - cline <- paste(cline, collapse = " ") + line <- rep_len(line, length(cline)) + keep <- trimws(header_df$header) != "" + cline <- cline[keep] + line <- line[keep] + cline <- paste(cline[line], collapse = " ") return(cline) } diff --git a/inst/NEWS.md b/inst/NEWS.md index 91d760a..2a7b677 100644 --- a/inst/NEWS.md +++ b/inst/NEWS.md @@ -1,11 +1,13 @@ -kableExtra 1.4.0.8 +kableExtra 1.4.0.9 -------------------------------------------------------------------------------- New Features: * Added `show_every_page` argument to `footnote()` (#867). * Added `class` argument to `cell_spec()` (#871). +* `add_header_above()` now supports specifying +`line` as a vector (#700). Bug Fixes: diff --git a/man/add_header_above.Rd b/man/add_header_above.Rd index a858a50..5a80085 100644 --- a/man/add_header_above.Rd +++ b/man/add_header_above.Rd @@ -73,7 +73,7 @@ options including \code{xx-small}, \code{x-small}, \code{small}, \code{medium}, \item{escape}{A T/F value showing whether special characters should be escaped.} -\item{line}{A T/F value to control whether a line will appear underneath the +\item{line}{A T/F value/vector to control whether a line will appear underneath the header} \item{line_sep}{A numeric value indicating how much the midlines should be @@ -96,7 +96,7 @@ function and adds an header row on top of it. } \examples{ \dontrun{ -x <- knitr::kable(head(mtcars), "html") +x <- kbl(head(mtcars), "html") # Add a row of header with 3 columns on the top of the table. The column # span for the 2nd and 3rd one are 5 & 6. add_header_above(x, c(" ", "Group 1" = 5, "Group 2" = 6))