Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improving multirow handling in collapse_rows() #851

Merged
merged 6 commits into from
Jul 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading