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, "}")