From 785de85ecb7b61d0b95ace5f5f1e822d81121aa5 Mon Sep 17 00:00:00 2001 From: Vincent Arel-Bundock Date: Sun, 3 Nov 2024 07:02:22 -0500 Subject: [PATCH] break theme but fix some tabularray styles --- R/build_tt.R | 4 ++++ R/class.R | 2 ++ R/group_tabularray.R | 4 +++- R/style_tabularray.R | 11 ++++++++--- R/theme_tt.R | 5 +++-- 5 files changed, 20 insertions(+), 6 deletions(-) diff --git a/R/build_tt.R b/R/build_tt.R index cb5cf600..8f45f7c2 100644 --- a/R/build_tt.R +++ b/R/build_tt.R @@ -17,6 +17,10 @@ build_tt <- function(x, output = NULL) { x@output <- output + # for (th in x@lazy_theme) { + # x <- do.call(th[[1]], th[2]) + # } + # groups must increment indices here for (idx in seq_along(x@lazy_group)) { l <- x@lazy_group[[idx]] diff --git a/R/class.R b/R/class.R index 8b41834e..f67cf2d2 100644 --- a/R/class.R +++ b/R/class.R @@ -49,6 +49,7 @@ setClass( lazy_style = "list", lazy_plot = "list", lazy_finalize = "list", + lazy_theme = "list", group_tt_i = "logical", portable = "logical" ) @@ -84,6 +85,7 @@ setMethod("initialize", "tinytable", function( .Object@portable <- FALSE .Object@style <- data.frame() .Object@group_tt_i <- FALSE + .Object@lazy_theme <- list(list(identity, list())) # conditional: allows NULL user input if (!is.null(placement)) .Object@placement <- placement if (!is.null(caption)) .Object@caption <- caption diff --git a/R/group_tabularray.R b/R/group_tabularray.R index 99285579..d047d858 100644 --- a/R/group_tabularray.R +++ b/R/group_tabularray.R @@ -46,13 +46,15 @@ group_tabularray_col <- function(x, j, ihead, ...) { for (k in seq_along(j)) { z <- min(j[[k]]) + cs <- max(j[[k]]) - min(j[[k]]) + 1 + if (cs == 1) cs <- NULL args <- list( tt_build_now = TRUE, x = x, i = ihead, j = z, align = "c", - colspan = max(j[[k]]) - min(j[[k]]) + 1) + colspan = cs) x <- do.call(style_tt, args) } diff --git a/R/style_tabularray.R b/R/style_tabularray.R index 81294590..1612abcd 100644 --- a/R/style_tabularray.R +++ b/R/style_tabularray.R @@ -78,7 +78,7 @@ setMethod( if (trimws(cmd) != "") set[idx] <- sprintf(tmp, set[idx], cmd) fontsize <- sty$fontsize[row] - if (is.na(is.numeric(fontsize))) { + if (!is.na(as.numeric(fontsize))) { set[idx] <- sprintf( "%s font=\\fontsize{%sem}{%sem}\\selectfont,", set[idx], fontsize, fontsize + 0.3) @@ -94,6 +94,11 @@ setMethod( } } + alignv <- sty$alignv[row] + if (!is.na(alignv)) { + set[idx] <- sprintf("%s, valign=%s,", set[idx], alignv) + } + indent <- sty$indent[row] if (isTRUE(indent > 0)) { set[idx] <- sprintf("%s preto={\\hspace{%sem}},", set[idx], indent) @@ -122,7 +127,7 @@ setMethod( rec$set <- clean(set) rec$span <- clean(span) - rec <- rec[rec$set != "", , drop = FALSE] + rec <- rec[rec$set != "" | rec$span != "", , drop = FALSE] recj <- split(rec, rec$j) for (rj in recj) { @@ -141,7 +146,7 @@ setMethod( rj$set[1]) x@table_string <- tabularray_insert(x@table_string, content = spec, type = "inner") } else { - if (rj$set[1] != "") { + if (rj$set[1] != "" | rj$span[1] != "") { spec <- sprintf("cell{%s}{%s}={%s}{%s}", paste(rj$i, collapse = ","), rj$j[1], diff --git a/R/theme_tt.R b/R/theme_tt.R index 2f62791e..80de62ca 100644 --- a/R/theme_tt.R +++ b/R/theme_tt.R @@ -407,7 +407,8 @@ theme_tt <- function(x, theme, ...) { na <- unique(sort(names(td))) assert_choice(theme, na) fn <- td[[theme]] - out <- fn(x, ...) - return(out) + out <- list(list(fn, list(...))) + x@lazy_theme <- c(x@lazy_theme, out) + return(x) }