From 4eccfda7af707e7aea3d1890f266d3f636999abc Mon Sep 17 00:00:00 2001 From: Vincent Arel-Bundock Date: Fri, 13 Dec 2024 21:56:08 -0500 Subject: [PATCH] issue355v3 --- NAMESPACE | 4 ++-- R/print.R | 2 +- R/sanity.R | 35 ++++++++++++++++++++++++++++++++++ R/style_bootstrap.R | 38 +++++++++++++++++++++++++++++++++++-- man/knit_print.tinytable.Rd | 2 +- 5 files changed, 75 insertions(+), 6 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index 21186b69..b7394fa6 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,10 +1,10 @@ # Generated by roxygen2: do not edit by hand -S3method(knit_print,tinytable) -S3method(knitr::knit_print,tinytable) +S3method(knitr::knit_print, tinytable) S3method(print,tinytable) export(format_tt) export(group_tt) +export(knit_print.tinytable) export(plot_tt) export(save_tt) export(style_tt) diff --git a/R/print.R b/R/print.R index 42edc957..ccfecb0c 100644 --- a/R/print.R +++ b/R/print.R @@ -2,7 +2,7 @@ #' #' @keywords internal #' @return A string with class `knit_asis` to be printed in Rmarkdown or Quarto documents. -#' @exportS3Method knitr::knit_print +#' @rawNamespace S3method(knitr::knit_print, tinytable) #' @export knit_print.tinytable <- function(x, output = get_option("tinytable_print_output", default = NULL), diff --git a/R/sanity.R b/R/sanity.R index ff119a2f..c0a07a7f 100644 --- a/R/sanity.R +++ b/R/sanity.R @@ -448,3 +448,38 @@ sanity_num_mark <- function(digits, num_mark_big, num_mark_dec) { if (num_mark_dec != ".") stop("`num_mark_dec` requires a `digits` value.", call. = FALSE) } } + +get_deleted_cells <- function(sty) { + # If no spanning cells, return empty data frame + if (!any(!is.na(sty$colspan) | !is.na(sty$rowspan))) { + return(data.frame(row = integer(0), col = integer(0))) + } + + # Get rows with spanning cells + span_rows <- which(!is.na(sty$colspan) | !is.na(sty$rowspan)) + + deleted_cells <- lapply(span_rows, function(idx) { + colspan <- sty$colspan[idx] + rowspan <- sty$rowspan[idx] + row <- sty$row[idx] + col <- sty$col[idx] + + # Convert NA to 1 for non-spanning dimension + colspan <- if(is.na(colspan)) 1L else as.integer(colspan) + rowspan <- if(is.na(rowspan)) 1L else as.integer(rowspan) + + # Create grid of all spanned cells + grid <- expand.grid( + row = row:(row + rowspan - 1), + col = col:(col + colspan - 1) + ) + + # Remove the original cell + grid <- grid[!(grid$row == row & grid$col == col), ] + + return(grid) + }) + + # Combine all deleted cells + do.call(rbind, deleted_cells) +} diff --git a/R/style_bootstrap.R b/R/style_bootstrap.R index a557b4ee..ae5d46e3 100644 --- a/R/style_bootstrap.R +++ b/R/style_bootstrap.R @@ -51,13 +51,23 @@ setMethod( ) css <- rep("", nrow(rec)) + del <- get_deleted_cells(sty) + if (nrow(del) > 0) { + rec <- merge(rec, del, all.x = TRUE,sort = FALSE) + } else { + rec$tinytable_deleted_cell <- FALSE + } + for (row in seq_len(nrow(sty))) { # index: sty vs rec idx_i <- sty$i[row] if (is.na(idx_i)) idx_i <- unique(rec$i) idx_j <- sty$j[row] if (is.na(idx_j)) idx_j <- unique(rec$j) - idx <- rec$i == idx_i & rec$j == idx_j + idx <- idx_line <- (rec$i %in% idx_i & rec$j %in% idx_j) + + # deleted cells with rowspan or colspan are not styled but have lines + idx[which(rec$tinytable_deleted_cell)] <- FALSE if (isTRUE(sty[row, "bold"])) css[idx] <- paste(css[idx], "font-weight: bold;") if (isTRUE(sty[row, "italic"])) css[idx] <- paste(css[idx], "font-style: italic;") @@ -96,7 +106,7 @@ setMethod( if (template != "") { lin <- paste(lin, sprintf(template, line_color, line_width)) } - css[idx] <- paste(css[idx], lin) + css[idx_line] <- paste(css[idx_line], lin) } css <- gsub(" +", " ", trimws(css)) @@ -106,6 +116,7 @@ setMethod( rec$j <- rec$j - 1 + # spans: before styles because we return(x) if there is no style for (row in seq_len(nrow(sty))) { rowspan <- if (!is.na(sty$rowspan[row])) sty$rowspan[row] else 1 @@ -146,3 +157,26 @@ setMethod( return(x) } ) + + + + +get_deleted_cells <- function(sty) { + out <- sty[which(sty$rowspan > 1 | sty$colspan > 1), ] + if (nrow(out) == 0) return(out) + out$rowspan <- ifelse(is.na(out$rowspan), 1, out$rowspan) + out$colspan <- ifelse(is.na(out$colspan), 1, out$colspan) + out <- lapply(seq_len(nrow(out)), function(k) { + row <- out[k, , drop = FALSE] + del <- expand.grid( + i = row$i:(row$i + row$rowspan - 1), + j = row$j:(row$j + row$colspan - 1) + ) + del <- del[!(del$i == row$i & del$j == row$j), ] + return(del) + }) + out <- do.call(rbind, out) + out$tinytable_deleted_cell <- TRUE + sty <- merge(sty, out, all.x = TRUE) + return(out) +} diff --git a/man/knit_print.tinytable.Rd b/man/knit_print.tinytable.Rd index 11888660..e3927911 100644 --- a/man/knit_print.tinytable.Rd +++ b/man/knit_print.tinytable.Rd @@ -4,7 +4,7 @@ \alias{knit_print.tinytable} \title{Print a tinytable object in knitr} \usage{ -\method{knit_print}{tinytable}( +knit_print.tinytable( x, output = get_option("tinytable_print_output", default = NULL), ...