add hline options to collapse_rows
diff --git a/R/collapse_rows.R b/R/collapse_rows.R
index 4f901fb..1f9d0d3 100644
--- a/R/collapse_rows.R
+++ b/R/collapse_rows.R
@@ -8,13 +8,16 @@
#'
#' @param kable_input Output of `knitr::kable()` with `format` specified
#' @param columns Numeric column positions where rows need to be collapsed.
+#' @param latex_hline Option controlling the behavior of adding hlines to table.
+#' Choose from `full`, `major`, `none`.
#'
#' @examples dt <- data.frame(a = c(1, 1, 2, 2), b = c("a", "a", "a", "b"))
#' x <- knitr::kable(dt, "html")
#' collapse_rows(x)
#'
#' @export
-collapse_rows <- function(kable_input, columns = NULL) {
+collapse_rows <- function(kable_input, columns = NULL,
+ latex_hline = c("full", "major", "none")) {
# if (is.null(columns)) {
# stop("Please specify numeric positions of columns you want to collapse.")
# }
@@ -27,7 +30,8 @@
return(collapse_rows_html(kable_input, columns))
}
if (kable_format == "latex") {
- return(collapse_rows_latex(kable_input, columns))
+ latex_hline <- match.arg(latex_hline, c("full", "major", "none"))
+ return(collapse_rows_latex(kable_input, columns, latex_hline))
}
}
@@ -85,16 +89,10 @@
return(mapping_matrix)
}
-collapse_rows_latex <- function(kable_input, columns) {
+collapse_rows_latex <- function(kable_input, columns, latex_hline) {
table_info <- magic_mirror(kable_input)
out <- enc2utf8(as.character(kable_input))
- # if (table_info$duplicated_rows) {
- # dup_fx_out <- fix_duplicated_rows_latex(out, table_info)
- # out <- dup_fx_out[[1]]
- # table_info <- dup_fx_out[[2]]
- # }
-
if (is.null(columns)) {
columns <- seq(1, table_info$ncol)
}
@@ -129,10 +127,20 @@
new_contents <- c()
for (i in seq(1:nrow(collapse_matrix))) {
new_contents[i] <- paste0(new_kable_dt[i, ], collapse = " & ")
- table_info$contents[i+1] <- new_contents[i]
+ table_info$contents[i + 1] <- new_contents[i]
if (i != nrow(collapse_matrix)) {
- row_midrule <- midline_groups(which(as.numeric(midrule_matrix[i, ]) > 0),
- table_info$booktabs)
+ row_midrule <- switch(
+ latex_hline,
+ "none" = "",
+ "full" = midline_groups(which(as.numeric(midrule_matrix[i, ]) > 0),
+ table_info$booktabs),
+ "major" = ifelse(
+ sum(as.numeric(midrule_matrix[i, ]) > 0) == ncol(midrule_matrix),
+ 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)
@@ -168,7 +176,7 @@
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))
+ end_indexes <- c(start_indexes - 1, length(x))
ranges <- paste0(x[start_indexes], "-", x[end_indexes])
if (booktabs) {
out <- paste0("\\\\cmidrule{", ranges, "}")
diff --git a/docs/awesome_table_in_pdf.Rmd b/docs/awesome_table_in_pdf.Rmd
index acd8a75..fe9e7f6 100644
--- a/docs/awesome_table_in_pdf.Rmd
+++ b/docs/awesome_table_in_pdf.Rmd
@@ -327,7 +327,9 @@
```
## Group rows via multi-row cell
-Function `group_rows` is great for showing simple structural information on rows but sometimes people may need to show structural information with multiple layers. When it happens, you may consider to use `collapse_rows` instead, which will put repeating cells in columns into multi-row cells. If you even need to specify column/row format, use `column_spec` & `row_spec` before you pipe it into `collapse_rows`.
+Function `group_rows` is great for showing simple structural information on rows but sometimes people may need to show structural information with multiple layers. When it happens, you may consider to use `collapse_rows` instead, which will put repeating cells in columns into multi-row cells.
+
+In LaTeX, `collapse_rows` adds some extra hlines to help differentiate groups. You can customize this behavior using the `latex_hline` argument. You can choose from `full` (default), `major` and `none`.
```{r}
collapse_rows_dt <- data.frame(C1 = c(rep("a", 10), rep("b", 5)),
@@ -336,7 +338,7 @@
C4 = sample(c(0,1), 15, replace = TRUE))
kable(collapse_rows_dt, format = "latex", booktabs = T, align = "c") %>%
column_spec(1, bold=T) %>%
- collapse_rows(columns = 1:2)
+ collapse_rows(columns = 1:2, latex_hline = "major")
```
```{r}
diff --git a/docs/awesome_table_in_pdf.pdf b/docs/awesome_table_in_pdf.pdf
index 832e929..c9c5f0f 100644
--- a/docs/awesome_table_in_pdf.pdf
+++ b/docs/awesome_table_in_pdf.pdf
Binary files differ