Skip to content

Commit

Permalink
multi group_tt works in LaTeX
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentarelbundock committed Jan 30, 2024
1 parent 61d9bb4 commit 52f789b
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 13 deletions.
7 changes: 3 additions & 4 deletions R/build_tt.R
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,10 @@ build_tt <- function(x, output = NULL) {
out <- eval(lazy_tt)
out <- meta(out, "output", output)

# group the table (before style)
for (l in m$lazy_group) {
# nhead needs to be changed insider the loop so that we have indices for each new row
out <- meta(out, "nhead", meta(out, "nhead") + 1)
for (idx in seq_along(m$lazy_group)) {
l <- m$lazy_group[[idx]]
l[["x"]] <- out
l[["ihead"]] <- -1 * idx
if (output == "html") {
l[[1]] <- quote(group_bootstrap)
} else if (output == "latex") {
Expand Down
11 changes: 5 additions & 6 deletions R/group_tabularray.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
group_tabularray <- function(x, i, j, indent) {
group_tabularray <- function(x, i, j, indent, ...) {
out <- x
# columns first to count headers properly
if (!is.null(j)) {
out <- group_tabularray_col(out, j)
out <- group_tabularray_col(out, j, ...)
}
if (!is.null(i)) {
out <- group_tabularray_row(out, i, indent)
Expand All @@ -11,7 +11,8 @@ group_tabularray <- function(x, i, j, indent) {
}


group_tabularray_col <- function(x, j, i) {
group_tabularray_col <- function(x, j, ihead, ...) {

m <- meta(x)

out <- strsplit(x, split = "\\n")[[1]]
Expand Down Expand Up @@ -47,9 +48,7 @@ group_tabularray_col <- function(x, j, i) {
args <- list(
tt_build_now = TRUE,
x = out,
# the new header is always first row and
# style_tt always adds nhead to index
i = eval(1 - meta(out)$nhead),
i = ihead,
j = z,
align = "c",
colspan = max(j[[k]]) - min(j[[k]]) + 1)
Expand Down
7 changes: 4 additions & 3 deletions R/group_tt.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#' @inheritParams style_tt
#' @param i A named list of row indices to group. The names of the list will be used as labels.
#' @param j A named list of column indices to group. The names of the list will be used as labels. See examples below. Note: empty labels must be a space: " ".
#' @param ... Other arguments are ignored.
#' @return An object of class `tt` representing the table.
#' @param indent integer number of `pt` to use when indenting the non-labelled rows.
#' @details
Expand All @@ -19,7 +20,8 @@
#' j = list(
#' "Foo" = 2:3,
#' "Bar" = 4:5))
group_tt <- function(x, i = NULL, j = NULL, indent = 1) {
group_tt <- function(x, i = NULL, j = NULL, indent = 1, ...) {
# ... is important for ihead passing

if (is.null(meta(x))) stop("`x` must be generated by `tinytable::tt()`.", call. = FALSE)
if (is.null(i) && is.null(j)) stop("At least one of `i` or `j` must be specified.", call. = FALSE)
Expand All @@ -31,8 +33,7 @@ group_tt <- function(x, i = NULL, j = NULL, indent = 1) {
j <- sanitize_group_index(j, hi = attr(x, "ncol"), orientation = "column")

if (!is.null(i)) out <- meta(out, "nrows", meta(out, "nrows") + length(i))
# nhead needs to be changed in build_tt() so that we have indices for each new
# row when in the process of cashing in the lazy eval groups.
if (!is.null(j)) out <- meta(out, "nhead", meta(out, "nhead") + 1)

# we don't need this as a list, and we use some sorting later
i <- unlist(i)
Expand Down
25 changes: 25 additions & 0 deletions inst/tinytest/_tinysnapshot/group_tt-3level_tex.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

\begin{table}
\centering
\begin{tblr}[ %% tabularray outer open
] %% tabularray outer close
{ %% tabularray inner open
colspec={Q[]Q[]Q[]Q[]Q[]},
cell{3}{2}={c=2,}{halign=c,},
cell{3}{4}={c=2,}{halign=c,},
cell{2}{1}={c=2,}{halign=c,},
cell{2}{3}={c=3,}{halign=c,},
cell{1}{1}={c=3,}{halign=c,},
cell{1}{4}={c=1,}{halign=c,},
} %% tabularray inner close
\toprule
e & & & f & \\ \cmidrule[lr]{1-3}\cmidrule[lr]{4-4}
c & & d & & \\ \cmidrule[lr]{1-2}\cmidrule[lr]{3-5}
& a & & b & \\ \cmidrule[lr]{2-3}\cmidrule[lr]{4-5}
mpg & cyl & disp & hp & drat \\ \midrule %% TinyTableHeader
21 & 6 & 160 & 110 & 3.9 \\
21 & 6 & 160 & 110 & 3.9 \\
22.8 & 4 & 108 & 93 & 3.85 \\
\bottomrule
\end{tblr}
\end{table}
11 changes: 11 additions & 0 deletions inst/tinytest/test-group_tt.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,14 @@ tab <- tt(x) |>
group_tt(j = list("e" = 1:3, "f" = 4))
expect_snapshot_print(tab, label = "group_tt-3level_md")
options(tinytable_print_output = NULL)


# 3 level: latex
options(tinytable_print_output = "latex")
x <- mtcars[1:3, 1:5]
tab <- tt(x) |>
group_tt(j = list("a" = 2:3, "b" = 4:5)) |>
group_tt(j = list("c" = 1:2, "d" = 3:5)) |>
group_tt(j = list("e" = 1:3, "f" = 4))
expect_snapshot_print(tab, label = "group_tt-3level_tex")
options(tinytable_print_output = NULL)

0 comments on commit 52f789b

Please sign in to comment.