From 3660ffc9d534a4390ce631e220972f54e086aec8 Mon Sep 17 00:00:00 2001 From: Vincent Arel-Bundock Date: Fri, 20 Sep 2024 21:53:20 -0400 Subject: [PATCH] style_tt() output conditional + typst default theme --- DESCRIPTION | 2 +- NEWS.md | 1 + R/build_tt.R | 7 +++++-- R/style_tt.R | 10 ++++++++-- R/theme_tt.R | 22 +++++++--------------- man/style_tt.Rd | 3 +++ sandbox/typst.qmd | 1 - 7 files changed, 25 insertions(+), 21 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 10baad1d..ed7acf3b 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -2,7 +2,7 @@ Package: tinytable Type: Package Title: Simple and Configurable Tables in 'HTML', 'LaTeX', 'Markdown', 'Word', 'PNG', 'PDF', and 'Typst' Formats Description: Create highly customized tables with this simple and dependency-free package. Data frames can be converted to 'HTML', 'LaTeX', 'Markdown', 'Word', 'PNG', 'PDF', or 'Typst' tables. The user interface is minimalist and easy to learn. The syntax is concise. 'HTML' tables can be customized using the flexible 'Bootstrap' framework, and 'LaTeX' code with the 'tabularray' package. -Version: 0.4.0.1 +Version: 0.4.0.2 Imports: methods Depends: diff --git a/NEWS.md b/NEWS.md index 90d42b45..2b0f5c7d 100644 --- a/NEWS.md +++ b/NEWS.md @@ -3,6 +3,7 @@ ## Development * The `i` argument in `style_tt()` now accepts a logical matrix of same dimensions as `x`, to style specific cells, rather than all combinations of `i` and `j` vectors. Thanks to @dhicks for the feature request #329. +* `style_tt()` gets new `output` argument for conditional styling based on output format. ## 0.4.0 diff --git a/R/build_tt.R b/R/build_tt.R index 83448417..91753b2d 100644 --- a/R/build_tt.R +++ b/R/build_tt.R @@ -87,7 +87,7 @@ build_tt <- function(x, output = NULL) { } if (x@output == "typst") { - if (is.null(x@theme[[1]]) || is.function(x@theme[[1]]) || isTRUE("default" %in% x@theme[[1]])) { + if (is.null(x@theme[[1]]) || is.function(x@theme[[1]]) || isTRUE(x@theme[[1]] %in% c("default", "striped"))) { # reverse the order of the lines to allow overwriting defaults ls <- x@lazy_style x <- style_tt(x, i = -x@nhead + 1, line = "t", line_width = 0.1) @@ -100,7 +100,10 @@ build_tt <- function(x, output = NULL) { if (!x@output %in% c("markdown", "gfm", "dataframe")) { for (l in x@lazy_style) { l[["x"]] <- x - x <- eval(l) + # output-specific styling + if (is.null(l$output) || isTRUE(x@output == l$output)) { + x <- eval(l) + } } } diff --git a/R/style_tt.R b/R/style_tt.R index 948cd3d3..49ef0a9f 100644 --- a/R/style_tt.R +++ b/R/style_tt.R @@ -46,6 +46,7 @@ #' @param bootstrap_css_rule String. Complete CSS rules (with curly braces, semicolon, etc.) that apply to the table class specified by the `bootstrap_class` argument. #' @param tabularray_inner A string that specifies the "inner" settings of a tabularray LaTeX table. #' @param tabularray_outer A string that specifies the "outer" settings of a tabularray LaTeX table. +#' @param output Apply style only to the output format specified by this argument. `NULL` means that we apply to all formats. #' @param ... extra arguments are ignored #' @return An object of class `tt` representing the table. #' @template latex_preamble @@ -144,10 +145,13 @@ style_tt <- function (x, bootstrap_class = NULL, bootstrap_css = NULL, bootstrap_css_rule = NULL, + output = NULL, ...) { out <- x + assert_choice(output, c("typst", "latex", "html", "markdown", "gfm"), null.ok = TRUE) + if ("width" %in% names(list(...))) { stop("The `width` argument is now in the `tt()` function.", call. = FALSE) } @@ -181,7 +185,8 @@ style_tt <- function (x, tabularray_outer = tabularray_outer, bootstrap_class = bootstrap_class, bootstrap_css = bootstrap_css, - bootstrap_css_rule = bootstrap_css_rule) + bootstrap_css_rule = bootstrap_css_rule, + output = output) if (isTRUE(list(...)[["tt_build_now"]])) { out <- eval(cal) @@ -222,7 +227,8 @@ style_tt_lazy <- function (x, tabularray_outer, bootstrap_class, bootstrap_css, - bootstrap_css_rule) { + bootstrap_css_rule, + output) { out <- x diff --git a/R/theme_tt.R b/R/theme_tt.R index 1cdd460b..4c452975 100644 --- a/R/theme_tt.R +++ b/R/theme_tt.R @@ -156,23 +156,15 @@ theme_grid <- function(x, ...) { theme_striped <- function(x, ...) { assert_class(x, "tinytable") - fn <- function(table) { - if (isTRUE(table@output == "typst")) { - table <- style_eval(table, i = 1 - table@nhead, line = "t", line_width = 0.1) - table <- style_eval(table, i = 0, line = "b", line_width = 0.05) - table <- style_eval(table, i = nrow(table), line = "b", line_width = 0.1) - table <- style_eval(table, i = 1 - table@nhead, line = "t", line_width = 0.1) - table <- style_eval(table, i = 0, line = "b", line_width = 0.05) - table <- style_eval(table, i = nrow(table), line = "b", line_width = 0.1) - table <- style_eval(table, i = seq(1, nrow(table), by = 2), background = "#ededed") - } - return(table) - } x <- style_tt(x, - finalize = fn, tabularray_inner = "row{even}={bg=black!5!white}", - bootstrap_class = "table table-striped") - x <- theme_tt(x, "placement") + bootstrap_class = "table table-striped", + output = "latex") + x <- style_tt(x, + i = seq(1, nrow(x), by = 2), + background = "#ededed", + output = "typst") + x <- theme_tt(x, "default") return(x) } diff --git a/man/style_tt.Rd b/man/style_tt.Rd index b9cb4e10..1ca6d356 100644 --- a/man/style_tt.Rd +++ b/man/style_tt.Rd @@ -30,6 +30,7 @@ style_tt( bootstrap_class = NULL, bootstrap_css = NULL, bootstrap_css_rule = NULL, + output = NULL, ... ) } @@ -110,6 +111,8 @@ style_tt( \item{bootstrap_css_rule}{String. Complete CSS rules (with curly braces, semicolon, etc.) that apply to the table class specified by the \code{bootstrap_class} argument.} +\item{output}{Apply style only to the output format specified by this argument. \code{NULL} means that we apply to all formats.} + \item{...}{extra arguments are ignored} } \value{ diff --git a/sandbox/typst.qmd b/sandbox/typst.qmd index 0da53788..d45fedac 100644 --- a/sandbox/typst.qmd +++ b/sandbox/typst.qmd @@ -36,7 +36,6 @@ tab <- tt(x, theme = "striped") tab ``` - ```{r} # Formatting dat <- data.frame(