Skip to content

Commit

Permalink
nhead is at top after lazy
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentarelbundock committed Jan 30, 2024
1 parent 8f9e622 commit 3898255
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 7 deletions.
1 change: 1 addition & 0 deletions R/build_tt.R
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ build_tt <- function(x, output = NULL) {
m$lazy_style <- rev(m$lazy_style)
}
if (output != "markdown") {
browser()
for (l in m$lazy_style) {
l[["x"]] <- out
out <- eval(l)
Expand Down
4 changes: 4 additions & 0 deletions R/group_bootstrap.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ group_bootstrap <- function(x, i, j, indent = 1, ...) {
group_bootstrap_col <- function(x, i, j, ...) {

m <- meta(x)

# nhead needs to be changed insider the loop so that we have indices for each new row
m$nhead <- m$nhead + 1

out <- strsplit(x, "\\n")[[1]]
header <- NULL

Expand Down
9 changes: 7 additions & 2 deletions R/group_grid.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ group_grid <- function(x, i = NULL, j = NULL, ...) {

group_grid_col <- function(x, j, ...) {
m <- meta(x)

# nhead needs to be changed insider the loop so that we have indices for each new row
m$nhead <- m$nhead + 1

# columns
header <- empty_cells(j)
cw <- meta(x, "col_widths")
Expand All @@ -24,10 +28,11 @@ group_grid_col <- function(x, j, ...) {
out <- strsplit(out, split = "\\n")[[1]]
out <- out[out != "\\n"]
out <- out[!out %in% c("\\n", "")]
out <- out[2]
x <- strsplit(x, split = "\\n")[[1]]
x <- x[!x %in% c("\\n", "")]
out <- out[1:(length(out) - 1)]
out <- paste(c(out, x), collapse = "\n")
x <- c(x[1], out, x)
out <- paste(x, collapse = "\n")
attr(out, "tinytable_meta") <- m
class(out) <- class(x)
return(out)
Expand Down
6 changes: 4 additions & 2 deletions R/group_tabularray.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ group_tabularray <- function(x, i, j, indent) {
}


group_tabularray_col <- function(x, j) {
group_tabularray_col <- function(x, j, i) {
m <- meta(x)
# nhead needs to be changed insider the loop so that we have indices for each new row
m$nhead <- m$nhead + 1

out <- strsplit(x, split = "\\n")[[1]]

Expand Down Expand Up @@ -49,7 +51,7 @@ group_tabularray_col <- function(x, j) {
x = out,
# the new header is always first row and
# style_tt always adds nhead to index
i = 1 - meta(out)$nhead,
i = eval(1 - meta(out)$nhead),
j = z,
align = "c",
colspan = max(j[[k]]) - min(j[[k]]) + 1)
Expand Down
5 changes: 3 additions & 2 deletions R/group_tt.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#' @inheritParams tt
#' @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..
#' @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: " ".
#' @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 @@ -31,7 +31,8 @@ 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))
if (!is.null(j)) out <- meta(out, "nhead", meta(out, "nhead") + 1)
# nhead needs to be changed insider the loop so that we have indices for each new row
# 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
4 changes: 4 additions & 0 deletions R/group_typst.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ group_typst <- function(x, i = NULL, j = NULL, ...) {

group_typst_col <- function(x, j, ...) {
m <- meta(x)

# nhead needs to be changed insider the loop so that we have indices for each new row
m$nhead <- m$nhead + 1

out <- x
miss <- as.list(setdiff(seq_len(m$ncols), unlist(j)))
miss <- stats::setNames(miss, rep(" ", length(miss)))
Expand Down
16 changes: 16 additions & 0 deletions inst/tinytest/_tinysnapshot/group_tt-3level_md.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

+------+-----+------+-----+------+
| e | f |
+------+-----+------+-----+------+
| c | d |
+------+-----+------+-----+------+
| | a | b |
+------+-----+------+-----+------+
| mpg | cyl | disp | hp | drat |
+======+=====+======+=====+======+
| 21 | 6 | 160 | 110 | 3.9 |
+------+-----+------+-----+------+
| 21 | 6 | 160 | 110 | 3.9 |
+------+-----+------+-----+------+
| 22.8 | 4 | 108 | 93 | 3.85 |
+------+-----+------+-----+------+
15 changes: 15 additions & 0 deletions inst/tinytest/test-group_tt.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
source("helpers.R")
using("tinysnapshot")




# 3 level: markdown
options(tinytable_print_output = "markdown")
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_md")
options(tinytable_print_output = NULL)
2 changes: 1 addition & 1 deletion man/group_tt.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 3898255

Please sign in to comment.