From 8d67ac4f7b2b850a5e6dd146d644dc973e4fc2de Mon Sep 17 00:00:00 2001 From: Vincent Arel-Bundock Date: Mon, 4 Nov 2024 15:49:00 -0500 Subject: [PATCH] issue #355 : rowspan change indicies in HTML by delete cells --- R/style_bootstrap.R | 27 +++++++++++++++++++++++++-- inst/tinytest/test-html.R | 11 ++++++++++- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/R/style_bootstrap.R b/R/style_bootstrap.R index 7c0c023a..fbd2b7b3 100644 --- a/R/style_bootstrap.R +++ b/R/style_bootstrap.R @@ -37,7 +37,6 @@ setMethod( sty <- x@style - sty$alignv[which(sty$alignv == "t")] <- "top" sty$alignv[which(sty$alignv == "b")] <- "bottom" sty$alignv[which(sty$alignv == "m")] <- "middle" @@ -122,11 +121,35 @@ setMethod( } } - rec$css_arguments <- css rec <- rec[rec$css_arguments != "", , drop = FALSE] if (nrow(rec) == 0) return(x) + # rowspan + colspan delete cells + bad <- data.frame() + idx <- which(!is.na(sty$i) & !is.na(sty$j) & (!is.na(sty$rowspan) | !is.na(sty$colspan))) + idx <- sty[idx, c("i", "j", "rowspan", "colspan"), drop = FALSE] + idx$i <- idx$i - 1 + x@nhead + idx$j <- idx$j - 1 + for (row in seq_len(nrow(idx))) { + imax <- idx[row, "i"] + jmax <- idx[row, "j"] + if (!is.na(idx[row, "rowspan"])) { + imax <- imax + idx[row, "rowspan"] - 1 + } + if (!is.na(idx[row, "colspan"])) { + jmax <- jmax + idx[row, "colspan"] - 1 + } + tmp <- expand.grid(i = seq(idx[row, "i"], imax), j = seq(idx[row, "j"], jmax), deleted = TRUE) + tmp <- tmp[tmp$i != idx[row, "i"] | tmp$j != idx[row, "j"],] + bad <- rbind(bad, tmp) + } + if (nrow(bad) > 0) { + rec <- merge(rec, bad, by = c("i", "j"), all.x = TRUE, sort = FALSE) + rec <- rec[-which(rec$deleted == TRUE),] + } + + # Unique CSS arguments assigne by arrays css_table <- unique(rec[, c("css_arguments"), drop = FALSE]) css_table$id_css <- sapply(seq_len(nrow(css_table)), function(i) get_id(stem = "tinytable_css_")) diff --git a/inst/tinytest/test-html.R b/inst/tinytest/test-html.R index 4d1ff085..e937bee1 100644 --- a/inst/tinytest/test-html.R +++ b/inst/tinytest/test-html.R @@ -3,7 +3,6 @@ using("tinysnapshot") options(tinytable_print_output = "html") - x <- mtcars[1:4, 1:5] tab <- tt(x, theme = "striped") @@ -200,6 +199,16 @@ if (Sys.info()["user"] == "vincent") { options(op) } + +# Issue #355: indices are changed after cells are merged with rowspan +tab <- tt(head(iris)) |> + style_tt(j = 1, i = 1, rowspan = 2, colspan = 2) |> + style_tt(i = 1, color = "blue") |> + style_tt(j = 1, color = "orange", italic = TRUE) +expect_snapshot_print(print_html(tab, "html"), "html-issue355_colors_and_spans.html") + + + # # Built-in plots # # cannot be tested because the names of plots are random and set seed doesn't work # set.seed(1024)