Skip to content

Commit

Permalink
fix group_tt_row_tabularray
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentarelbundock committed Jan 15, 2024
1 parent d07af65 commit 932a1d1
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 8 deletions.
4 changes: 2 additions & 2 deletions R/group_tabularray.R
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ group_tabularray_row <- function(x, i, indent, ...) {

# store the original body lines when creating the table, and use those to guess the boundaries.
# a hack, but probably safer than most regex approaches I can think of.
body <- which(tab %in% m[["body"]])
body <- which(tab %in% m$body)
top <- tab[1:(min(body) - 1)]
mid <- tab[min(body):max(body)]
bot <- tab[(max(body) + 1):length(tab)]
Expand All @@ -91,7 +91,7 @@ group_tabularray_row <- function(x, i, indent, ...) {
class(tab) <- class(x)

cellspec <- sprintf("cell{%s}{%s}={%s}{%s},",
idx$new[is.na(idx$old)] + m$head,
idx$new[is.na(idx$old)] + m$nhead,
1,
paste0("c=", m$ncols),
""
Expand Down
12 changes: 8 additions & 4 deletions R/group_tt.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ group_tt <- function(x, i, j, indent = 1, ...) {
if (missing(i)) i <- NULL
if (missing(j)) j <- NULL

i <- sanitize_group_index(i, hi = attr(x, "nrow") + 1)
j <- sanitize_group_index(j, hi = attr(x, "ncol"))
i <- sanitize_group_index(i, hi = attr(x, "nrow") + 1, orientation = "row")
j <- sanitize_group_index(j, hi = attr(x, "ncol"), orientation = "column")

# we don't need this as a list, and we use some sorting later
i <- unlist(i)
Expand All @@ -41,11 +41,15 @@ group_tt <- function(x, i, j, indent = 1, ...) {



sanitize_group_index <- function(idx, hi) {
sanitize_group_index <- function(idx, hi, orientation) {
if (is.null(idx)) return(idx)
assert_list(idx, named = TRUE)
for (n in names(idx)) {
assert_integerish(idx[[n]], lower = 1, upper = hi, name = n)
if (orientation == "row") {
assert_integerish(idx[[n]], len = 1, lower = 1, upper = hi, name = n)
} else {
assert_integerish(idx[[n]], lower = 1, upper = hi, name = n)
}
}
if (anyDuplicated(unlist(idx)) > 0) stop("Duplicate group indices.", call. = FALSE)
out <- lapply(idx, function(x) min(x):max(x))
Expand Down
3 changes: 2 additions & 1 deletion R/style_tt.R
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ style_tt <- function (x,

if (m$output == "latex") {
# colspan requires cell level, so we keep the full settings DF
if (!is.null(colspan)) {
if (is.null(colspan)) {
if (is.null(i) && is.null(j)) {
settings <- unique(settings[, "i", drop = FALSE])
} else if (is.null(i)) {
Expand Down Expand Up @@ -242,6 +242,7 @@ style_tt <- function (x,
}


attr(out, "tinytable_meta") <- m
return(out)
}

Expand Down
4 changes: 3 additions & 1 deletion R/tt.R
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ tt <- function(x,
stop(msg, call. = FALSE)
}
for (col in seq_along(align)) {
out <- style_tt(out, i = 0:nrow(x), j = col, align = align[[col]])
# cleaner code if we do it in two shots for tabularray
out <- style_tt(out, j = col, align = align[[col]])
out <- style_tt(out, i = 0, j = col, align = align[[col]])
}
}

Expand Down

0 comments on commit 932a1d1

Please sign in to comment.