From eeb330fad060c24b27fffb7c0e8123d71d4a1859 Mon Sep 17 00:00:00 2001 From: unDocUMeantIt Date: Thu, 20 Jun 2024 11:45:52 +0200 Subject: [PATCH 1/6] improving multirow handling in collapse_rows() --- R/collapse_rows.R | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/R/collapse_rows.R b/R/collapse_rows.R index ab7186f..ae32c7d 100644 --- a/R/collapse_rows.R +++ b/R/collapse_rows.R @@ -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`. @@ -29,7 +29,7 @@ #' @param target If multiple columns are selected to do collapsing and a target #' column is specified, this target column will be used to collapse other #' columns based on the groups of this target column. -#' @param col_names T/F. A LaTeX specific option. If you set `col.names` be +#' @param col_names T/F. A LaTeX specific option. If you set `col_names` be #' `NULL` in your `kable` call, you need to set this option false to let #' everything work properly. #' @param longtable_clean_cut T/F with default T. Multirow cell sometimes are @@ -213,10 +213,8 @@ 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\\]") { - 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 + if (num_rows && num_cols && valign == "") { + vmove <- sum(rowSums(collapse_matrix[i - seq_len(num_rows-1), j + seq_len(num_cols), drop = FALSE]) > 0) * 0.5 } else vmove <- 0 new_kable_dt[i, columns[j]] <- collapse_new_dt_item( @@ -328,10 +326,13 @@ collapse_new_dt_item <- function(x, span, width = NULL, align, valign, vmove = 0 if (span == 0) return("") if (span == 1) return(x) out <- paste0( - "\\\\multirow", valign, "\\{", -span, "\\}\\{", + "\\\\multirow", valign, "\\{", -ifelse(valign != "\\[t\\]", span, span - 1), "\\}\\{", ifelse(is.null(width), "\\*", width), "\\}", - if(vmove) paste0("[", vmove, "\\\\dimexpr\\\\aboverulesep+\\\\belowrulesep+\\\\cmidrulewidth]"), + ifelse(vmove > 0, + paste0("[", vmove, "\\\\dimexpr\\\\aboverulesep+\\\\belowrulesep+\\\\cmidrulewidth]"), + paste0("[\\\\normalbaselineskip]"), + ), "\\{", switch(align, "l" = "\\\\raggedright\\\\arraybackslash ", From 7c69eaf53d95d791c5b8173a9f05fb87befe9a2b Mon Sep 17 00:00:00 2001 From: unDocUMeantIt Date: Thu, 20 Jun 2024 11:52:59 +0200 Subject: [PATCH 2/6] typo --- R/collapse_rows.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/collapse_rows.R b/R/collapse_rows.R index ae32c7d..9082e4a 100644 --- a/R/collapse_rows.R +++ b/R/collapse_rows.R @@ -331,7 +331,7 @@ collapse_new_dt_item <- function(x, span, width = NULL, align, valign, vmove = 0 "\\}", ifelse(vmove > 0, paste0("[", vmove, "\\\\dimexpr\\\\aboverulesep+\\\\belowrulesep+\\\\cmidrulewidth]"), - paste0("[\\\\normalbaselineskip]"), + paste0("[\\\\normalbaselineskip]") ), "\\{", switch(align, From bfe45577c9952e67ee769d86e971cd9e255b8766 Mon Sep 17 00:00:00 2001 From: unDocUMeantIt Date: Thu, 20 Jun 2024 13:36:00 +0200 Subject: [PATCH 3/6] revert argument name --- R/collapse_rows.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/collapse_rows.R b/R/collapse_rows.R index 9082e4a..735274a 100644 --- a/R/collapse_rows.R +++ b/R/collapse_rows.R @@ -29,7 +29,7 @@ #' @param target If multiple columns are selected to do collapsing and a target #' column is specified, this target column will be used to collapse other #' columns based on the groups of this target column. -#' @param col_names T/F. A LaTeX specific option. If you set `col_names` be +#' @param col_names T/F. A LaTeX specific option. If you set `col.names` be #' `NULL` in your `kable` call, you need to set this option false to let #' everything work properly. #' @param longtable_clean_cut T/F with default T. Multirow cell sometimes are From d6069ead395996d7d2df72d08d6a256ecdd5d6e6 Mon Sep 17 00:00:00 2001 From: unDocUMeantIt Date: Fri, 21 Jun 2024 09:48:36 +0200 Subject: [PATCH 4/6] fixed labels in collape_rows() for latex_hline %in% c("full", "major", "none", "linespace") --- R/collapse_rows.R | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/R/collapse_rows.R b/R/collapse_rows.R index 735274a..efe2819 100644 --- a/R/collapse_rows.R +++ b/R/collapse_rows.R @@ -213,14 +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 == "") { - vmove <- sum(rowSums(collapse_matrix[i - seq_len(num_rows-1), j + seq_len(num_cols), drop = FALSE]) > 0) * 0.5 - } else + 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 { 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 ) } } @@ -322,16 +326,18 @@ 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, "\\{", -ifelse(valign != "\\[t\\]", span, span - 1), "\\}\\{", + "\\\\multirow", valign, "\\{", -ifelse(any(valign != "\\[t\\]", !latex_hline %in% c("none", "major", "linespace")), span, span - 1), "\\}\\{", ifelse(is.null(width), "\\*", width), "\\}", - ifelse(vmove > 0, - paste0("[", vmove, "\\\\dimexpr\\\\aboverulesep+\\\\belowrulesep+\\\\cmidrulewidth]"), - paste0("[\\\\normalbaselineskip]") + switch( + latex_hline, + "full" = paste0("[", span - 1, "\\\\dimexpr\\\\aboverulesep+\\\\belowrulesep+\\\\cmidrulewidth]"), + "custom" = paste0("[", vmove, "\\\\dimexpr\\\\aboverulesep+\\\\belowrulesep+\\\\cmidrulewidth]"), + paste0("[\\\\normalbaselineskip]") ), "\\{", switch(align, From ad75e547ff0052f6751b2bdfc541f16c51391e8f Mon Sep 17 00:00:00 2001 From: unDocUMeantIt Date: Mon, 8 Jul 2024 22:02:46 +0200 Subject: [PATCH 5/6] shorter lines and less confusing ifelse() statement --- R/collapse_rows.R | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/R/collapse_rows.R b/R/collapse_rows.R index efe2819..2bb20aa 100644 --- a/R/collapse_rows.R +++ b/R/collapse_rows.R @@ -326,17 +326,32 @@ 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, latex_hline) { +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, "\\{", -ifelse(any(valign != "\\[t\\]", !latex_hline %in% c("none", "major", "linespace")), span, span - 1), "\\}\\{", + "\\\\multirow", valign, "\\{", + ifelse( + any( + valign != "\\[t\\]", + !latex_hline %in% c("none", "major", "linespace") + ), + -span, + -span - 1 + ), "\\}\\{", ifelse(is.null(width), "\\*", width), "\\}", switch( latex_hline, - "full" = paste0("[", span - 1, "\\\\dimexpr\\\\aboverulesep+\\\\belowrulesep+\\\\cmidrulewidth]"), - "custom" = paste0("[", vmove, "\\\\dimexpr\\\\aboverulesep+\\\\belowrulesep+\\\\cmidrulewidth]"), + "full" = paste0( + "[", span - 1, + "\\\\dimexpr\\\\aboverulesep+\\\\belowrulesep+\\\\cmidrulewidth]" + ), + "custom" = paste0( + "[", vmove, + "\\\\dimexpr\\\\aboverulesep+\\\\belowrulesep+\\\\cmidrulewidth]" + ), paste0("[\\\\normalbaselineskip]") ), "\\{", From 209416290686fce0df8d7a42184c1b9c49f8a225 Mon Sep 17 00:00:00 2001 From: unDocUMeantIt Date: Tue, 9 Jul 2024 08:28:29 +0200 Subject: [PATCH 6/6] fixed missing brackets --- R/collapse_rows.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/collapse_rows.R b/R/collapse_rows.R index 2bb20aa..bba69fa 100644 --- a/R/collapse_rows.R +++ b/R/collapse_rows.R @@ -338,7 +338,7 @@ collapse_new_dt_item <- function(x, span, width = NULL, align, valign, !latex_hline %in% c("none", "major", "linespace") ), -span, - -span - 1 + -(span - 1) ), "\\}\\{", ifelse(is.null(width), "\\*", width), "\\}",