Skip to content

Commit

Permalink
Merge pull request #851 from unDocUMeantIt/master
Browse files Browse the repository at this point in the history
improving multirow handling in collapse_rows()
  • Loading branch information
dmurdoch authored Jul 10, 2024
2 parents 3e46c17 + 2094162 commit cc2bd72
Showing 1 changed file with 33 additions and 11 deletions.
44 changes: 33 additions & 11 deletions R/collapse_rows.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
#' be separated by hlines.
#' @param row_group_label_position Option controlling positions of row group
#' labels. Choose from `identity`, `stack`, or `first` -- the latter behaves
#' like `identity` when `row_group_label_position` is `top` but without using
#' the multirow package.
#' like `identity` when `valign` is `top` but without using the multirow
#' package.
#' @param row_group_label_fonts A list of arguments that can be supplied to
#' group_rows function to format the row group label when
#' `row_group_label_position` is `stack`.
Expand Down Expand Up @@ -213,16 +213,18 @@ collapse_rows_latex <- function(kable_input, columns, latex_hline, valign,
# are to the right of this column
num_rows <- collapse_matrix[i, j]
num_cols <- ncol(collapse_matrix) - j
if (num_rows && num_cols && valign != "\\[b\\]") {
if (all(num_rows > 0, num_cols > 0, valign != "\\[b\\]", latex_hline != "none")) {
vmove <- sum(rowSums(collapse_matrix[i - seq_len(num_rows-1), j + seq_len(num_cols), drop = FALSE]) > 0)
if (valign == "")
vmove <- 0.5*vmove
} else
if(valign == "") {
vmove <- 0.5 * vmove
}
} else {
vmove <- 0
}
new_kable_dt[i, columns[j]] <- collapse_new_dt_item(
kable_dt[i, columns[j]], collapse_matrix[i, j], column_width,
x = kable_dt[i, columns[j]], span = collapse_matrix[i, j], width = column_width,
align = column_align, valign = valign,
vmove = vmove
vmove = vmove, latex_hline = latex_hline
)
}
}
Expand Down Expand Up @@ -324,14 +326,34 @@ kable_dt_latex <- function(x, col_names) {
data.frame(do.call(rbind, str_split(x, " & ")), stringsAsFactors = FALSE)
}

collapse_new_dt_item <- function(x, span, width = NULL, align, valign, vmove = 0) {
collapse_new_dt_item <- function(x, span, width = NULL, align, valign,
vmove = 0, latex_hline) {
if (span == 0) return("")
if (span == 1) return(x)
out <- paste0(
"\\\\multirow", valign, "\\{", -span, "\\}\\{",
"\\\\multirow", valign, "\\{",
ifelse(
any(
valign != "\\[t\\]",
!latex_hline %in% c("none", "major", "linespace")
),
-span,
-(span - 1)
), "\\}\\{",
ifelse(is.null(width), "\\*", width),
"\\}",
if(vmove) paste0("[", vmove, "\\\\dimexpr\\\\aboverulesep+\\\\belowrulesep+\\\\cmidrulewidth]"),
switch(
latex_hline,
"full" = paste0(
"[", span - 1,
"\\\\dimexpr\\\\aboverulesep+\\\\belowrulesep+\\\\cmidrulewidth]"
),
"custom" = paste0(
"[", vmove,
"\\\\dimexpr\\\\aboverulesep+\\\\belowrulesep+\\\\cmidrulewidth]"
),
paste0("[\\\\normalbaselineskip]")
),
"\\{",
switch(align,
"l" = "\\\\raggedright\\\\arraybackslash ",
Expand Down

0 comments on commit cc2bd72

Please sign in to comment.