Skip to content

Commit

Permalink
break theme but fix some tabularray styles
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentarelbundock committed Nov 3, 2024
1 parent da25cd3 commit 785de85
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 6 deletions.
4 changes: 4 additions & 0 deletions R/build_tt.R
Original file line number Diff line number Diff line change
Expand Up @@ -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]]
Expand Down
2 changes: 2 additions & 0 deletions R/class.R
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ setClass(
lazy_style = "list",
lazy_plot = "list",
lazy_finalize = "list",
lazy_theme = "list",
group_tt_i = "logical",
portable = "logical"
)
Expand Down Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion R/group_tabularray.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
11 changes: 8 additions & 3 deletions R/style_tabularray.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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) {
Expand All @@ -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],
Expand Down
5 changes: 3 additions & 2 deletions R/theme_tt.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit 785de85

Please sign in to comment.