From 1e37b78f060896f0ba2bd7192807624f07bbb7b0 Mon Sep 17 00:00:00 2001 From: Vincent Arel-Bundock Date: Fri, 19 Jan 2024 08:02:33 -0500 Subject: [PATCH] group_grid seems to work --- R/group_tt.R | 4 +++- R/tt.R | 2 +- R/tt_grid.R | 27 ++++++++++++++++----------- 3 files changed, 20 insertions(+), 13 deletions(-) diff --git a/R/group_tt.R b/R/group_tt.R index c3664186..f5eae4cb 100644 --- a/R/group_tt.R +++ b/R/group_tt.R @@ -7,7 +7,7 @@ group_tt <- function(x, i = NULL, j = NULL, indent = 1) { if (is.null(meta(x))) stop("`x` must be generated by `tinytable::tt()`.", call. = FALSE) - if (!isTRUE(meta(x, "output") %in% c("html", "latex"))) return(x) + if (!isTRUE(meta(x, "output") %in% c("html", "latex", "markdown"))) return(x) if (is.null(i) && is.null(j)) stop("At least one of `i` or `j` must be specified.", call. = FALSE) assert_integerish(indent, lower = 0) @@ -26,6 +26,8 @@ group_tt <- function(x, i = NULL, j = NULL, indent = 1) { cal <- call("group_tabularray", i = i, j = j, indent = indent) } else if (meta(out)$output == "html") { cal <- call("group_bootstrap", i = i, j = j, indent = indent) + } else if (meta(out)$output == "markdown") { + cal <- call("group_grid", i = i, j = j) } out <- meta(out, "lazy_group", c(meta(out)$lazy_group, list(cal))) diff --git a/R/tt.R b/R/tt.R index 985b3218..90061146 100644 --- a/R/tt.R +++ b/R/tt.R @@ -77,7 +77,7 @@ tt <- function(x, cal <- call("tt_bootstrap", x = out, caption = caption, theme = theme, width = width, notes = notes) } else { - cal <- call("tt_markdown", x = out, caption = caption) + cal <- call("tt_grid", x = out, caption = caption) } out <- meta(out, "lazy_tt", cal) diff --git a/R/tt_grid.R b/R/tt_grid.R index 9092a353..b3f75192 100755 --- a/R/tt_grid.R +++ b/R/tt_grid.R @@ -1,4 +1,3 @@ - grid_line <- function(col_widths, char = "-") { line_sep <- lapply(col_widths, function(k) strrep(char, k)) line_sep <- paste(line_sep, collapse = "+") @@ -6,7 +5,14 @@ grid_line <- function(col_widths, char = "-") { return(line_sep) } -tt_grid <- function(x, col_widths = NULL) { + +tt_grid <- function(x, col_widths = NULL, ...) { + + m <- meta(x) + + if (is.null(col_widths)) { + col_widths <- m$col_widths + } tab <- as.matrix(x) if (!is.null(names(x))) { @@ -44,14 +50,18 @@ tt_grid <- function(x, col_widths = NULL) { } out <- paste(tab, collapse = "\n") - attr(out, "col_widths") <- col_widths + + # rebuild output + attr(out, "tinytable_meta") <- m + out <- meta(out, "col_widths", col_widths) out <- meta(out, "output", "grid") + class(out) <- c("tinytable", "knitr_kable") + # output return(out) } - empty_cells <- function(lst) { # Find the largest number in the list max_num <- max(unlist(lst)) @@ -74,9 +84,9 @@ empty_cells <- function(lst) { } -group_grid <- function(x, j) { +group_grid <- function(x, j, ...) { header <- empty_cells(j) - cw <- attr(x, "col_widths") + cw <- meta(x, "col_widths") cw <- sapply(header, function(k) sum(cw[k]) + length(cw[k]) - 1) txt <- t(matrix(names(cw))) out <- tt_grid(txt, cw) @@ -100,8 +110,3 @@ df <- data.frame( ) -pkgload::load_all() -x <- tt_grid(df) -j = list("foo" = 2:3, "bar" = 4:5) -z = group_grid(x, j) -cat(z)