Skip to content

Commit

Permalink
style_tt() output conditional + typst default theme
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentarelbundock committed Sep 21, 2024
1 parent f8ceae1 commit 3660ffc
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 21 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
7 changes: 5 additions & 2 deletions R/build_tt.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
}
}
}

Expand Down
10 changes: 8 additions & 2 deletions R/style_tt.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -222,7 +227,8 @@ style_tt_lazy <- function (x,
tabularray_outer,
bootstrap_class,
bootstrap_css,
bootstrap_css_rule) {
bootstrap_css_rule,
output) {

out <- x

Expand Down
22 changes: 7 additions & 15 deletions R/theme_tt.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
3 changes: 3 additions & 0 deletions man/style_tt.Rd

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

1 change: 0 additions & 1 deletion sandbox/typst.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ tab <- tt(x, theme = "striped")
tab
```


```{r}
# Formatting
dat <- data.frame(
Expand Down

0 comments on commit 3660ffc

Please sign in to comment.