Merge pull request #418 from kbrevoort/align_header
Allow non-scalar values of align argument to `add_header_above` function
diff --git a/R/add_header_above.R b/R/add_header_above.R
index 93d5bda..1dde0ec 100644
--- a/R/add_header_above.R
+++ b/R/add_header_above.R
@@ -71,6 +71,14 @@
"for details.")
return(kable_input)
}
+ if ((length(align) != 1L) & (length(align) != length(header))) {
+ warning("Length of align vector supplied to add_header_above must either be 1 ",
+ "or the same length as the header supplied. The length of the align ",
+ sprintf("vector supplied is %i and the header length is %i.",
+ length(align), length(header)),
+ "Using default of centering each element of row.")
+ align <- 'c'
+ }
if (is.null(header)) return(kable_input)
if (is.data.frame(header)){
if(ncol(header) == 2 & is.character(header[[1]]) & is.numeric(header[[2]])){
@@ -174,11 +182,10 @@
color, background, font_size,
angle, line, line_sep, extra_css,
include_empty) {
- if (align %in% c("l", "c", "r")) {
- align <- switch(align, r = "right", c = "center", l = "left")
- }
+ align <- vapply(align, switch_align, 'x', USE.NAMES = FALSE)
+
row_style <- paste0(
- paste0("text-align: ", align, "; "),
+ "text-align: %s; ",
ifelse(bold, "font-weight: bold; ", ""),
ifelse(italic, "font-style: italic; ", ""),
ifelse(monospace, "font-family: monospace; ", ""),
@@ -227,6 +234,8 @@
line_sep <- ez_rep(line_sep, nrow(header_df))
line_sep <- glue::glue('padding-left:{line_sep}px;padding-right:{line_sep}px;')
+ row_style <- sprintf(row_style, align)
+
header_items <- ifelse(
trimws(header_df$header) == "",
paste0('<th style="border-bottom:hidden" colspan="', header_df$colspan,
@@ -271,7 +280,7 @@
header$header <- input_escape(header$header, align)
}
- align <- match.arg(align, c("c", "l", "r"))
+ align <- vapply(align, match.arg, 'a', choices = c("l", "c", "r"))
hline_type <- switch(table_info$booktabs + 1, "\\\\hline", "\\\\toprule")
new_header_split <- pdfTable_new_header_generator(
@@ -379,4 +388,10 @@
return(cline)
}
+switch_align <- function(x) {
+ if (x %in% c('l', 'c', 'r')) {
+ return(switch(x, l = 'left', c = 'center', r = 'right'))
+ }
+ return(x)
+}
diff --git a/R/linebreak.R b/R/linebreak.R
index 54d7cae..3bc0d51 100644
--- a/R/linebreak.R
+++ b/R/linebreak.R
@@ -14,7 +14,7 @@
linebreaker = "\n") {
if (is.numeric(x) | is.logical(x)) return(x)
x <- as.character(x)
- align <- match.arg(align, c("l", "c", "r"))
+ align <- vapply(align, match.arg, 'a', choices = c("l", "c", "r"))
if (double_escape) {
ifelse(str_detect(x, linebreaker),
paste0("\\\\makecell[", align, "]{",