bug fix for collapse rows html
diff --git a/R/collapse_rows.R b/R/collapse_rows.R
index a4293e6..0004ec8 100644
--- a/R/collapse_rows.R
+++ b/R/collapse_rows.R
@@ -1,6 +1,6 @@
-#' Collapse repeat rows to multirow cell
+#' Collapse repeated rows to multirow cell
#'
-#' @description Experimenting. Don't use it in production.
+#' @description
#' @export
collapse_rows <- function(kable_input, columns = NULL) {
# if (is.null(columns)) {
@@ -33,24 +33,20 @@
for (i in 1:nrow(collapse_matrix)) {
matrix_row <- collapse_matrix[i, ]
- if (sum(matrix_row) != length(matrix_row)) {
- target_row <- xml_child(kable_tbody, i)
- row_node_rm_count <- 0
- for (j in 1:length(matrix_row)) {
- if (matrix_row[j] != 1) {
- collapsing_col <- as.numeric(sub("x", "", names(matrix_row)[j])) -
- row_node_rm_count
- target_cell <- xml_child(target_row, collapsing_col)
- if (matrix_row[j] == 0) {
- xml_remove(target_cell)
- row_node_rm_count <- row_node_rm_count + 1
- } else {
- xml_attr(target_cell, "rowspan") <- matrix_row[j]
- xml_attr(target_cell, "style") <- paste0(
- xml_attr(target_cell, "style"),
- "vertical-align: middle !important;")
- }
- }
+ target_row <- xml_child(kable_tbody, i)
+ row_node_rm_count <- 0
+ for (j in 1:length(matrix_row)) {
+ collapsing_col <- as.numeric(sub("x", "", names(matrix_row)[j])) -
+ row_node_rm_count
+ target_cell <- xml_child(target_row, collapsing_col)
+ if (matrix_row[j] == 0) {
+ xml_remove(target_cell)
+ row_node_rm_count <- row_node_rm_count + 1
+ } else if (matrix_row[j] != 1) {
+ xml_attr(target_cell, "rowspan") <- matrix_row[j]
+ xml_attr(target_cell, "style") <- paste0(
+ xml_attr(target_cell, "style"),
+ "vertical-align: middle !important;")
}
}
}
diff --git a/tests/visual_tests/collapse_rows_html.Rmd b/tests/visual_tests/collapse_rows_html.Rmd
index 9722d3d..9ca589e 100644
--- a/tests/visual_tests/collapse_rows_html.Rmd
+++ b/tests/visual_tests/collapse_rows_html.Rmd
@@ -6,6 +6,7 @@
```{r}
library(knitr)
library(kableExtra)
+set.seed(100)
collapse_rows_dt <- data.frame(C1 = c(rep("a", 10), rep("b", 5)),
C2 = c(rep("c", 7), rep("d", 3), rep("c", 2), rep("d", 3)),
C3 = 1:15,
diff --git a/tests/visual_tests/column_row_spec_html.Rmd b/tests/visual_tests/column_row_spec_html.Rmd
index aee5f70..919e4e7 100644
--- a/tests/visual_tests/column_row_spec_html.Rmd
+++ b/tests/visual_tests/column_row_spec_html.Rmd
@@ -38,10 +38,10 @@
)
kable(dt, "html") %>%
- kable_styling(full_width = F) %>%
+ kable_styling(full_width = F) %>%
+ row_spec(1, bold = T) %>%
column_spec(2, "5cm", bold = T) %>%
- column_spec(4, "3cm", italic = T) %>%
- row_spec(1, bold = T)
+ column_spec(4, "3cm", italic = T)
```
```{r}