add midline/cline support to collapse_rows
diff --git a/NAMESPACE b/NAMESPACE
index 961edc0..905a515 100644
--- a/NAMESPACE
+++ b/NAMESPACE
@@ -3,7 +3,6 @@
export("%>%")
export(add_footnote)
export(add_header_above)
-export(add_header_left)
export(add_indent)
export(collapse_rows)
export(column_spec)
diff --git a/R/collapse_rows.R b/R/collapse_rows.R
index bf0e036..72ee31c 100644
--- a/R/collapse_rows.R
+++ b/R/collapse_rows.R
@@ -90,10 +90,6 @@
if (is.null(columns)) {
columns <- seq(1, table_info$ncol)
}
- 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
kable_dt <- kable_dt_latex(contents)
@@ -112,10 +108,26 @@
)
}
}
+
+ midrule_matrix <- collapse_row_matrix(kable_dt, seq(1, table_info$ncol),
+ html = F)
+ midrule_matrix[setdiff(seq(1, table_info$ncol), columns)] <- 1
+
+ ex_bottom <- length(contents) - 1
+ contents[2:ex_bottom] <- paste0(contents[2:ex_bottom], "\\\\\\\\")
+ if (!table_info$booktabs) {
+ contents[2:ex_bottom] <- paste0(contents[2:ex_bottom], "\n\\\\hline")
+ }
for (i in seq(1:nrow(collapse_matrix))) {
new_contents[i] <- paste0(new_kable_dt[i, ], collapse = " & ")
+ if (i != nrow(collapse_matrix)) {
+ row_midrule <- midline_groups(which(as.numeric(midrule_matrix[i, ]) > 0),
+ table_info$booktabs)
+ new_contents[i] <- paste0(new_contents[i], "\\\\\\\\\n", row_midrule)
+ }
out <- sub(contents[i + 1], new_contents[i], out)
}
+ out <- sub("\\\\addlinespace\n", "", out)
out <- structure(out, format = "latex", class = "knitr_kable")
table_info$collapse_rows <- TRUE
@@ -142,3 +154,17 @@
)
return(out)
}
+
+midline_groups <- function(x, booktabs = T) {
+ diffs <- c(1, diff(x))
+ start_indexes <- c(1, which(diffs > 1))
+ end_indexes <- c(start_indexes-1, length(x))
+ ranges <- paste0(x[start_indexes], "-", x[end_indexes])
+ if (booktabs) {
+ out <- paste0("\\\\cmidrule{", ranges, "}")
+ } else {
+ out <- paste0("\\\\cline{", ranges, "}")
+ }
+ out <- paste0(out, collapse = "\n")
+ return(out)
+}
diff --git a/docs/awesome_table_in_pdf.Rmd b/docs/awesome_table_in_pdf.Rmd
index f75b87c..6ba45ac 100644
--- a/docs/awesome_table_in_pdf.Rmd
+++ b/docs/awesome_table_in_pdf.Rmd
@@ -266,3 +266,9 @@
collapse_rows(columns = 1:2)
```
+```{r}
+kable(collapse_rows_dt, "latex", align = "c") %>%
+ column_spec(1, bold=T) %>%
+ collapse_rows()
+```
+
diff --git a/docs/awesome_table_in_pdf.pdf b/docs/awesome_table_in_pdf.pdf
index c8c4ded..97fdb0a 100644
--- a/docs/awesome_table_in_pdf.pdf
+++ b/docs/awesome_table_in_pdf.pdf
Binary files differ
diff --git a/man/add_header_left.Rd b/man/add_header_left.Rd
index 79847f8..43094a6 100644
--- a/man/add_header_left.Rd
+++ b/man/add_header_left.Rd
@@ -42,5 +42,4 @@
\examples{
x <- knitr::kable(head(mtcars), "html")
add_header_left(x, c("A" = 2, "B" = 2, "C" = 2))
-
}
diff --git a/tests/visual_tests/collapse_rows_html.Rmd b/tests/visual_tests/collapse_rows_html.Rmd
index 3f50879..6f26229 100644
--- a/tests/visual_tests/collapse_rows_html.Rmd
+++ b/tests/visual_tests/collapse_rows_html.Rmd
@@ -8,7 +8,7 @@
library(kableExtra)
set.seed(100)
collapse_rows_dt <- data.frame(C1 = c(rep("a", 10), rep("b", 5)),
- C2 = c(rep("c", 7), rep("d", 3), rep("c", 2), rep("d", 3)),
+ C2 = c(rep("c", 7), rep("d", 4), rep("c", 2), rep("d", 2)),
C3 = 1:15,
C4 = sample(c(0,1), 15, replace = TRUE))
kable(collapse_rows_dt, "html", align = "c") %>%
diff --git a/tests/visual_tests/collapse_rows_pdf.Rmd b/tests/visual_tests/collapse_rows_pdf.Rmd
index a2f1138..deb4f39 100644
--- a/tests/visual_tests/collapse_rows_pdf.Rmd
+++ b/tests/visual_tests/collapse_rows_pdf.Rmd
@@ -1,17 +1,18 @@
---
title: "Untitled"
-output: pdf_document
+output:
+ pdf_document:
+ keep_tex: true
---
```{r}
library(knitr)
library(kableExtra)
collapse_rows_dt <- data.frame(C1 = c(rep("aaaaaa aaaaaa", 10), rep("bbbbb bbbbbb", 5)),
- C2 = c(rep("c", 7), rep("d", 3), rep("c", 2), rep("d", 3)),
+ C2 = c(rep("c", 7), rep("d", 4), rep("c", 2), rep("d", 2)),
C3 = 1:15,
C4 = sample(c(0,1), 15, replace = TRUE))
kable(collapse_rows_dt, "latex", align = "c", booktabs = T) %>%
- kable_styling() %>%
- column_spec(1, bold=T, width = "2em") %>%
- collapse_rows(1)
+ column_spec(1, bold = T, width = "5em") %>%
+ collapse_rows(1:2)
```