Skip to content

Commit

Permalink
Allow line to be a vector in add_header_above for LaTeX as well a…
Browse files Browse the repository at this point in the history
…s HTML. Fixes #700.
  • Loading branch information
dmurdoch committed Dec 5, 2024
1 parent 7c844d4 commit e8db016
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 13 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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 = '[email protected]', role = c('aut', 'cre'),
comment = c(ORCID = '0000-0002-3386-6076')),
Expand Down
21 changes: 12 additions & 9 deletions R/add_header_above.R
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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))
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)]
Expand All @@ -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)
}

Expand Down
4 changes: 3 additions & 1 deletion inst/NEWS.md
Original file line number Diff line number Diff line change
@@ -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:

Expand Down
4 changes: 2 additions & 2 deletions man/add_header_above.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit e8db016

Please sign in to comment.