Skip to content

Commit

Permalink
Merge pull request #878 from haozhu233/addheaderabove
Browse files Browse the repository at this point in the history
Allow `align = "p{2cm}"` and similar in `add_header_above()`
  • Loading branch information
dmurdoch authored Dec 7, 2024
2 parents d3a90df + 17ce243 commit 3d51278
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 15 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.10
Version: 1.4.0.11
Authors@R: c(
person('Hao', 'Zhu', email = '[email protected]', role = c('aut', 'cre'),
comment = c(ORCID = '0000-0002-3386-6076')),
Expand Down
9 changes: 5 additions & 4 deletions R/add_header_above.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@
#' @param strikeout A T/F value to control whether the text of the selected row
#' need to be struck out.
#' @param align A character string for cell alignment. For HTML, possible values could
#' be `l`, `c`, `r` plus `left`, `center`, `right`, `justify`, `initial` and `inherit`
#' while for LaTeX, you can only choose from `l`, `c` & `r`.
#' be `l`, `c`, `r` plus `left`, `center`, `right`, `justify`, `initial` and `inherit`. All
#' legal values should be accepted for LaTeX, e.g.
#' `l`, `c`, `r`, `p{2cm}`, but the more exotic ones
#' may cause problems. If so, please post a bug
#' report!
#' @param color A character string/vector for text color. Here please pay
#' attention to the differences in color codes between HTML and LaTeX.
#' @param background A character string/vector for background color. Here please
Expand Down Expand Up @@ -321,8 +324,6 @@ pdfTable_add_header_above <- function(kable_input, header, bold, italic,
header$header <- gsub("\\\\", "\\\\\\\\", header$header)
}

align <- vapply(align, match.arg, 'a', choices = c("l", "c", "r"))

hline_type <- switch(table_info$booktabs + 1, "(\\\\hline)", toprule_regexp)
new_header_split <- pdfTable_new_header_generator(
header, table_info$booktabs, bold, italic, monospace, underline, strikeout,
Expand Down
14 changes: 9 additions & 5 deletions R/linebreak.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,23 @@
#' can have linebreaks in their table
#'
#' @param x A character vector
#' @param align Choose from "l", "c" or "r". Defaults to "l".
#' @param align Alignment of the text
#' @param double_escape Whether special character should be double escaped.
#' Default is FALSE.
#' @param linebreaker Symbol for linebreaks to replace. Default is `\n`.
#' @details
#' Not all `align` settings are supported, but
#' simple ones such as `l`, `c`, `r` should be fine.
#' More exotic ones like `p{3cm}` should work too,
#' but may not if they are too complicated.
#'
#'
#' @export
linebreak <- function(x, align = c("l", "c", "r"), double_escape = F,
linebreak <- function(x, align = "l", double_escape = F,
linebreaker = "\n") {
if (is.numeric(x) | is.logical(x)) return(x)
x <- as.character(x)
if (missing(align))
align <- "l"
align <- vapply(align, match.arg, 'a', choices = c("l", "c", "r"))

if (double_escape) {
ifelse(str_detect(x, linebreaker),
paste0("\\\\makecell[", align, "]{",
Expand Down
4 changes: 3 additions & 1 deletion inst/NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

kableExtra 1.4.0.10
kableExtra 1.4.0.11
--------------------------------------------------------------------------------

New Features:
Expand All @@ -8,6 +8,8 @@ New Features:
* Added `class` argument to `cell_spec()` (#871).
* `add_header_above()` now supports specifying
`line` as a vector (#700).
* `add_header_above()` and `linebreak()` now accept more exotic
`align` specifications, e.g. `p{2cm}`.

Bug Fixes:

Expand Down
7 changes: 5 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.

10 changes: 8 additions & 2 deletions man/linebreak.Rd

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

22 changes: 22 additions & 0 deletions tests/testthat/_snaps/add_header_above.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# add_header_above accepts p{3cm}

Code
kbl(mtcars[1:3, 1:3], "latex") %>% add_header_above(c(" ",
`This is a very long header that will need to be wrapped` = 3), align = c("l",
"p{3cm}"))
Output
\begin{tabular}[t]{l|r|r|r}
\hline
\multicolumn{1}{l|}{ } & \multicolumn{3}{p{3cm}}{This is a very long header that will need to be wrapped} \\
\cline{2-4}
& mpg & cyl & disp\\
\hline
Mazda RX4 & 21.0 & 6 & 160\\
\hline
Mazda RX4 Wag & 21.0 & 6 & 160\\
\hline
Datsun 710 & 22.8 & 4 & 108\\
\hline
\end{tabular}

9 changes: 9 additions & 0 deletions tests/testthat/test-add_header_above.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
test_that("add_header_above accepts p{3cm}", {
expect_snapshot(
kbl(
mtcars[1:3, 1:3], 'latex'
) %>%
add_header_above(c(' ',
'This is a very long header that will need to be wrapped' = 3),
align = c('l', 'p{3cm}')))
})

0 comments on commit 3d51278

Please sign in to comment.