add add_header_left
diff --git a/R/add_header_left.R b/R/add_header_left.R
index b91a38c..6fa6971 100644
--- a/R/add_header_left.R
+++ b/R/add_header_left.R
@@ -3,7 +3,8 @@
#' @description Experimenting. Please don't use it in production
#'
#' @export
-add_header_left <- function(kable_input, header = NULL, header_name = "") {
+add_header_left <- function(kable_input, header = NULL, header_name = "",
+ align = "c") {
if (is.null(header)) return(kable_input)
kable_format <- attr(kable_input, "format")
if (!kable_format %in% c("html", "latex")) {
@@ -11,31 +12,33 @@
"generic markdown table using pandoc is not supported.")
}
if (kable_format == "html") {
- return(add_header_left_html(kable_input, header, header_name))
+ return(add_header_left_html(kable_input, header, header_name, align))
}
if (kable_format == "latex") {
- return(add_header_left_latex(kable_input, header, header_name))
+ return(add_header_left_latex(kable_input, header, header_name, align))
}
}
# HTML
-add_header_left_html <- function(kable_input, header, header_name) {
+add_header_left_html <- function(kable_input, header, header_name, align) {
kable_attrs <- attributes(kable_input)
kable_xml <- read_xml(as.character(kable_input), options = "COMPACT")
kable_thead <- xml_tpart(kable_xml, "thead")
kable_tbody <- xml_tpart(kable_xml, "tbody")
+ align <- switch(align, "c" = "center", "l" = "left", "r" = "right")
+
new_header <- paste0(
- '<th style="text-align:center;" rowspan="',
+ '<th style="text-align:', align, '; vertical-align: bottom;" rowspan="',
length(xml_children(kable_thead)), '">', header_name, '</th>'
)
new_header <- read_xml(new_header, options = c("COMPACT"))
xml_add_child(xml_child(kable_thead, 1), new_header, .where = 0)
- header <- standardize_rowheader_input(header, length(xml_children(kable_tbody)))
+ header <- standardize_header(header, length(xml_children(kable_tbody)))
for (i in 1:nrow(header)) {
new_row_item <- paste0(
- '<td style="text-align:center; vertical-align: middle;" rowspan="',
+ '<td style="text-align:', align, '; vertical-align: middle;" rowspan="',
header$rowspan[i], '">', header$header[i], '</td>')
new_row_item <- read_xml(new_row_item, options = "COMPACT")
target_row <- xml_child(kable_tbody, header$row[i])
@@ -48,7 +51,7 @@
return(out)
}
-standardize_rowheader_input <- function(header, n_row) {
+standardize_header <- function(header, n_row) {
header_names <- names(header)
if (is.null(header_names)) {
@@ -71,6 +74,100 @@
))
}
-add_header_left_latex <- function(kable_input, header, header_name) {
- return(kable_input)
+add_header_left_latex <- function(kable_input, header, header_name, align) {
+ table_info <- magic_mirror(kable_input)
+ usepackage_latex("multirow")
+ if (!table_info$booktabs) {
+ warning("add_header_left only supports LaTeX table with booktabs. Please",
+ " use kable(..., booktabs = T) in your kable function.")
+ }
+ out <- as.character(kable_input)
+ contents <- table_info$contents
+ header_name <- escape_latex(header_name)
+ header <- standardize_header(header, length(contents) - 1)
+ header$header <- escape_latex(header$header)
+ header$header <- gsub("\\\\", "\\\\\\\\", header$header)
+ header$row <- header$row + 1
+ header$row_end <- header$row + header$rowspan - 1
+
+ # Align
+ out <- sub(paste0(table_info$begin_tabular, "\\{"),
+ paste0(table_info$begin_tabular, "{", align,
+ ifelse(table_info$booktabs, "", "|")),
+ out, perl = T)
+
+ # Header
+ if (!is.null(table_info$new_header_row)) {
+ new_header_row <- table_info$new_header_row
+ for (i in 1:length(new_header_row)) {
+ out <- sub(regex_escape(new_header_row[i]),
+ paste0(" & ", new_header_row[i]), out)
+ cline_old <- cline_gen(table_info$header_df[[i]], table_info$booktabs)
+ cline_old <- regex_escape(cline_old)
+ table_info$header_df[[i]] <- rbind(
+ data.frame(header = " ", colspan = 1),
+ table_info$header_df[[i]]
+ )
+ cline_new <- cline_gen(table_info$header_df[[i]], table_info$booktabs)
+ out <- sub(cline_old, cline_new, out)
+ }
+ }
+ out <- sub(contents[1], paste0(header_name, " & ", contents[1]), out)
+ table_info$contents[1] <- paste0(header_name, " & ", contents[1])
+
+ # move existing midrules if exists
+ out_lines <- read_lines(out)
+ tbody_start_row <- which(out_lines == "\\midrule")
+ tbody_end_row <- which(out_lines == "\\bottomrule")
+ before_tbody <- out_lines[seq(1, tbody_start_row)]
+ tbody <- out_lines[seq(tbody_start_row + 1, tbody_end_row - 1)]
+ after_tbody <- out_lines[seq(tbody_end_row, length(out_lines))]
+
+ # Remove addlinespace in this case
+ tbody <- tbody[tbody != "\\addlinespace"]
+
+ midrule_exist <- str_sub(tbody, 1, 9) == "\\cmidrule"
+ if (sum(midrule_exist) > 0) {
+ existing_midrules <- which(midrule_exist)
+ tbody[existing_midrules] <- unlist(lapply(
+ tbody[existing_midrules], cmidrule_plus_one
+ ))
+ out <- paste0(c(before_tbody, tbody, after_tbody), collapse = "\n")
+ }
+
+ for (j in 1:nrow(header)) {
+ new_row_pre <- paste0(
+ "\\\\multirow\\{", -header$rowspan[j], "\\}\\{\\*\\}\\{", header$header[j], "\\} & "
+ )
+ new_row_text <- paste0(new_row_pre, contents[header$row_end[j]])
+ out <- sub(contents[header$row_end[j]], new_row_text, out)
+ table_info$contents[header$row_end[j]] <- new_row_text
+ if (j != nrow(header)) {
+ out <- sub(
+ paste0(contents[header$row_end[j]], "\\\\\\\\\n"),
+ paste0(contents[header$row_end[j]],
+ "\\\\\\\\\n\\\\cmidrule[0.5pt](l{2pt}r{2pt}){1-1}\n"),
+ out
+ )
+ }
+ }
+
+ for (k in setdiff(seq(2, length(contents)), header$row_end)) {
+ out <- sub(contents[k],
+ paste0(" & ", contents[k]),
+ out)
+ table_info$contents[k] <- paste0(" & ", contents[k])
+ }
+
+ out <- structure(out, format = "latex", class = "knitr_kable")
+ attr(out, "original_kable_meta") <- table_info
+ return(out)
+}
+
+cmidrule_plus_one <- function(x) {
+ start_pos <- as.numeric(str_match(x, "\\)\\{(.*)-")[2]) + 1
+ stop_pos <- as.numeric(str_match(x, "-(.*)\\}")[2]) + 1
+ return(
+ paste0("\\cmidrule[0.5pt](l{2pt}r{2pt}){", start_pos, "-", stop_pos, "}")
+ )
}