add line_sep to add_header_above
diff --git a/DESCRIPTION b/DESCRIPTION
index 955536c..ca44252 100644
--- a/DESCRIPTION
+++ b/DESCRIPTION
@@ -42,7 +42,8 @@
stats,
grDevices,
htmltools,
- rstudioapi
+ rstudioapi,
+ glue
Suggests:
testthat,
magick,
diff --git a/NAMESPACE b/NAMESPACE
index 2894e97..8c5dcea 100644
--- a/NAMESPACE
+++ b/NAMESPACE
@@ -37,6 +37,7 @@
export(usepackage_latex)
export(xtable2kable)
import(htmltools)
+importFrom(glue,glue)
importFrom(grDevices,col2rgb)
importFrom(knitr,asis_output)
importFrom(knitr,include_graphics)
diff --git a/R/add_header_above.R b/R/add_header_above.R
index 00d56d9..b561189 100644
--- a/R/add_header_above.R
+++ b/R/add_header_above.R
@@ -35,6 +35,8 @@
#' escaped.
#' @param line A T/F value 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.
#' @param extra_css An HTML only option. CSS defined here will be send to the
#' td cell.
#'
@@ -49,7 +51,8 @@
underline = FALSE, strikeout = FALSE,
align = "c", color = NULL, background = NULL,
font_size = NULL, angle = NULL,
- escape = TRUE, line = TRUE, extra_css = NULL) {
+ escape = TRUE, line = TRUE, line_sep = 3,
+ extra_css = NULL) {
kable_format <- attr(kable_input, "format")
if (!kable_format %in% c("html", "latex")) {
warning("Please specify format in kable. kableExtra can customize either ",
@@ -61,13 +64,13 @@
return(htmlTable_add_header_above(kable_input, header, bold, italic,
monospace, underline, strikeout,
align, color, background, font_size,
- angle, escape, line, extra_css))
+ angle, escape, line, line_sep, extra_css))
}
if (kable_format == "latex") {
return(pdfTable_add_header_above(kable_input, header, bold, italic,
monospace, underline, strikeout,
align, color, background,
- font_size, angle, escape, line))
+ font_size, angle, escape, line, line_sep))
}
}
@@ -75,7 +78,8 @@
htmlTable_add_header_above <- function(kable_input, header, bold, italic,
monospace, underline, strikeout,
align, color, background, font_size,
- angle, escape, line, extra_css) {
+ angle, escape, line, line_sep,
+ extra_css) {
if (is.null(header)) return(kable_input)
kable_attrs <- attributes(kable_input)
kable_xml <- read_kable_as_xml(kable_input)
@@ -96,8 +100,8 @@
}
new_header_row <- htmlTable_new_header_generator(
- header, bold, italic, monospace, underline, strikeout, align, line,
- color, background, font_size, angle, extra_css
+ header, bold, italic, monospace, underline, strikeout, align,
+ color, background, font_size, angle, line, line_sep, extra_css
)
xml_add_child(kable_xml_thead, new_header_row, .where = 0)
out <- as_kable_xml(kable_xml)
@@ -127,9 +131,9 @@
}
htmlTable_new_header_generator <- function(header_df, bold, italic, monospace,
- underline, strikeout, align, line,
+ underline, strikeout, align,
color, background, font_size,
- angle, extra_css) {
+ angle, line, line_sep, extra_css) {
if (align %in% c("l", "c", "r")) {
align <- switch(align, r = "right", c = "center", l = "left")
}
@@ -175,15 +179,16 @@
line <- ifelse(ez_rep(line, nrow(header_df)),
"border-bottom: 1px solid #ddd; padding-bottom: 5px; ", "")
+ line_sep <- ez_rep(line_sep, nrow(header_df))
+ line_sep <- glue::glue('padding-left:{line_sep}px;padding-right:{line_sep}px;')
header_items <- ifelse(
trimws(header_df$header) == "",
paste0('<th style="border-bottom:hidden" colspan="', header_df$colspan,
'"></th>'),
paste0(
- '<th style="border-bottom:hidden; ',
- 'padding-bottom:0; padding-left:3px;padding-right:3px;',
- row_style, '" colspan="',
+ '<th style="border-bottom:hidden; padding-bottom:0; ',
+ line_sep, row_style, '" colspan="',
header_df$colspan, '"><div style="', line, '">', header_df$header,
'</div></th>')
)
@@ -194,9 +199,9 @@
# Add an extra header row above the current header in a LaTeX table ------
pdfTable_add_header_above <- function(kable_input, header, bold, italic,
- monospace, underline, strikeout,
- align, color, background,
- font_size, angle, escape, line) {
+ monospace, underline, strikeout, align,
+ color, background, font_size, angle,
+ escape, line, line_sep) {
table_info <- magic_mirror(kable_input)
header <- standardize_header_input(header)
@@ -209,7 +214,7 @@
hline_type <- switch(table_info$booktabs + 1, "\\\\hline", "\\\\toprule")
new_header_split <- pdfTable_new_header_generator(
header, table_info$booktabs, bold, italic, monospace, underline, strikeout,
- align, color, background, font_size, angle)
+ align, color, background, font_size, angle, line_sep)
if (line) {
new_header <- paste0(new_header_split[1], "\n", new_header_split[2])
} else {
@@ -240,7 +245,8 @@
pdfTable_new_header_generator <- function(header_df, booktabs = FALSE,
bold, italic, monospace,
underline, strikeout, align,
- color, background, font_size, angle) {
+ color, background, font_size, angle,
+ line_sep) {
n <- nrow(header_df)
bold <- ez_rep(bold, n)
italic <- ez_rep(italic, n)
@@ -286,17 +292,19 @@
)
header_text <- paste(paste(header_items, collapse = " & "), "\\\\\\\\")
- cline <- cline_gen(header_df, booktabs)
+ cline <- cline_gen(header_df, booktabs, line_sep)
return(c(header_text, cline))
}
-cline_gen <- function(header_df, booktabs) {
+cline_gen <- function(header_df, booktabs, line_sep) {
cline_end <- cumsum(header_df$colspan)
cline_start <- c(0, cline_end) + 1
cline_start <- cline_start[-length(cline_start)]
- cline_type <- switch(booktabs + 1,
- "\\\\cline{",
- "\\\\cmidrule(l{2pt}r{2pt}){")
+ cline_type <- switch(
+ booktabs + 1,
+ "\\\\cline{",
+ glue::glue("\\\\cmidrule(l{[line_sep]pt}r{[line_sep]pt}){",
+ .open = "[", .close = "]"))
cline <- paste0(cline_type, cline_start, "-", cline_end, "}")
cline <- cline[trimws(header_df$header) != ""]
cline <- paste(cline, collapse = " ")
diff --git a/R/kableExtra-package.R b/R/kableExtra-package.R
index 8c254d9..12a3579 100644
--- a/R/kableExtra-package.R
+++ b/R/kableExtra-package.R
@@ -72,6 +72,7 @@
#' @importFrom stats ave
#' @importFrom grDevices col2rgb
#' @importFrom rstudioapi isAvailable viewer
+#' @importFrom glue glue
#' @import htmltools
#' @name kableExtra-package
#' @aliases kableExtra
diff --git a/R/linebreak.R b/R/linebreak.R
index 74403a2..e090a5e 100644
--- a/R/linebreak.R
+++ b/R/linebreak.R
@@ -7,7 +7,7 @@
#' @param align Choose from "l", "c" or "r"
#' @param double_escape Whether special character should be double escaped.
#' Default is FALSE.
-#' @param linebreaker Symbol for linebreaks to replace. Default is `\n`.
+#' @param linebreaker Symbol for linebreaks to replace. Default is `\\n`.
#'
#' @export
linebreak <- function(x, align = c("l", "c", "r"), double_escape = F,
diff --git a/man/add_header_above.Rd b/man/add_header_above.Rd
index 75f49fc..28ef579 100644
--- a/man/add_header_above.Rd
+++ b/man/add_header_above.Rd
@@ -8,7 +8,7 @@
italic = FALSE, monospace = FALSE, underline = FALSE,
strikeout = FALSE, align = "c", color = NULL, background = NULL,
font_size = NULL, angle = NULL, escape = TRUE, line = TRUE,
- extra_css = NULL)
+ line_sep = 3, extra_css = NULL)
}
\arguments{
\item{kable_input}{Output of \code{knitr::kable()} with \code{format} specified}
@@ -56,6 +56,9 @@
\item{line}{A T/F value to control whether a line will appear underneath the
header}
+\item{line_sep}{A numeric value indicating how much the midlines should be
+separated by space. Default is 3.}
+
\item{extra_css}{An HTML only option. CSS defined here will be send to the
td cell.}
}
diff --git a/man/linebreak.Rd b/man/linebreak.Rd
index d7581d6..01ab847 100644
--- a/man/linebreak.Rd
+++ b/man/linebreak.Rd
@@ -15,7 +15,7 @@
\item{double_escape}{Whether special character should be double escaped.
Default is FALSE.}
-\item{linebreaker}{Symbol for linebreaks to replace. Default is \code{\n}.}
+\item{linebreaker}{Symbol for linebreaks to replace. Default is \code{\\n}.}
}
\description{
This function generate LaTeX code of \code{makecell} so that users
diff --git a/tests/visual_tests/add_header_above_html.Rmd b/tests/visual_tests/add_header_above_html.Rmd
index ce953fe..4ea7657 100644
--- a/tests/visual_tests/add_header_above_html.Rmd
+++ b/tests/visual_tests/add_header_above_html.Rmd
@@ -26,7 +26,7 @@
kable(dt, format = "html") %>%
kable_styling(full_width = F,
bootstrap_options = c("striped", "bordered")) %>%
- add_header_above(c(" ", "a" = 2, "b" = 2)) %>%
+ add_header_above(c(" ", "a" = 2, "b" = 2), line_sep = c(10, 0, 10)) %>%
add_header_above(c(" ", "c" = 3, "d" = 1)) %>%
column_spec(1, "1in")
```
diff --git a/tests/visual_tests/add_header_above_pdf.Rmd b/tests/visual_tests/add_header_above_pdf.Rmd
index 4eba3f4..a518553 100644
--- a/tests/visual_tests/add_header_above_pdf.Rmd
+++ b/tests/visual_tests/add_header_above_pdf.Rmd
@@ -23,7 +23,7 @@
```{r}
kable(dt, format = "latex", booktabs = T) %>%
- add_header_above(c(" ", "a" = 2, "b" = 2))%>%
+ add_header_above(c(" ", "a" = 2, "b" = 2), line_sep = c(0, 5, 0))%>%
kable_styling()
```