Skip to content

Commit

Permalink
lazy style: order no longer matters in latex
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentarelbundock committed Jan 16, 2024
1 parent 6da64ab commit 540286d
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 21 deletions.
26 changes: 13 additions & 13 deletions R/print.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,19 @@
#' @export
knit_print.tinytable <- function(x, ...) {
# lazy styles get evaluated here, at the very end
x <- eval_style(x)
# not sure why we need to call this twice, but it appears necessary
out <- eval_style(x)
out <- eval_style(out)

if (meta(x)$output == "html") {
if (meta(out)$output == "html") {
# from htmltools:::html_preserve
# GPL3
inline <- grepl(x, "\n", fixed = TRUE)
inline <- grepl(out, "\n", fixed = TRUE)
if (inline) {
out <- sprintf("`%s`{=html}", x)
} else {
out <- sprintf("\n```{=html}\n%s\n```\n", x)
}

} else if (meta(x)$output %in% c("latex", "markdown")) {
out <- x
}

class(out) <- "knit_asis"
Expand All @@ -28,24 +27,25 @@ knit_print.tinytable <- function(x, ...) {
#' @export
print.tinytable <- function(x, ...) {
# lazy styles get evaluated here, at the very end
x <- eval_style(x)
# not sure why we need to call this twice, but it appears necessary
out <- eval_style(x)
out <- eval_style(out)

if (meta(x, "output") == "latex") {
out <- x
if (meta(out, "output") == "latex") {
class(out) <- "character"
cat("\n")
cat(out)
cat("\n")

} else if (meta(x, "output") == "markdown") {
} else if (meta(out, "output") == "markdown") {
cat("\n")
cat(x, sep = "\n")
cat(out, sep = "\n")

} else if (meta(x, "output") == "html") {
} else if (meta(out, "output") == "html") {
dir <- tempfile()
dir.create(dir)
htmlFile <- file.path(dir, "index.html")
cat(x, file = htmlFile)
cat(out, file = htmlFile)
if (isTRUE(check_dependency("rstudioapi")) && rstudioapi::isAvailable()) {
rstudioapi::viewer(htmlFile)
} else {
Expand Down
58 changes: 52 additions & 6 deletions R/style_tt.R
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,51 @@ style_tt <- function (x,
tabularray_outer = NULL,
bootstrap_css = NULL,
bootstrap_css_rule = NULL) {
out <- x
cal <- call("style_tt_lazy",
i = i,
j = j,
bold = bold,
italic = italic,
monospace = monospace,
underline = underline,
strikeout = strikeout,
color = color,
background = background,
fontsize = fontsize,
width = width,
align = align,
colspan = colspan,
indent = indent,
tabularray_inner = tabularray_inner,
tabularray_outer = tabularray_outer,
bootstrap_css = bootstrap_css,
bootstrap_css_rule = bootstrap_css_rule)
out <- meta(out, "lazy_style", c(meta(out)$lazy_style, list(cal)))
return(out)
}



style_tt_lazy <- function (x,
i,
j,
bold,
italic,
monospace,
underline,
strikeout,
color,
background,
fontsize,
width,
align,
colspan,
indent,
tabularray_inner,
tabularray_outer,
bootstrap_css,
bootstrap_css_rule) {

# sanity x
if (is.null(meta(x))) stop("`x` must be generated by `tinytable::tt()`.", call. = FALSE)
Expand All @@ -68,10 +113,10 @@ style_tt <- function (x,
if (is.null(j)) jval <- seq_len(meta(x)$nrows) else jval <- j

assert_style_tt(
x = x, i = i, j = j, ival = ival, jval = jval, bold = bold, italic = italic, monospace = monospace, underline = underline, strikeout = strikeout,
color = color, background = background, fontsize = fontsize, width = width, align = align, colspan = colspan, indent = indent,
tabularray_inner = tabularray_inner, tabularray_outer = tabularray_outer, bootstrap_css = bootstrap_css,
bootstrap_css_rule = bootstrap_css_rule)
x = x, i = i, j = j, ival = ival, jval = jval, bold = bold, italic = italic, monospace = monospace, underline = underline, strikeout = strikeout,
color = color, background = background, fontsize = fontsize, width = width, align = align, colspan = colspan, indent = indent,
tabularray_inner = tabularray_inner, tabularray_outer = tabularray_outer, bootstrap_css = bootstrap_css,
bootstrap_css_rule = bootstrap_css_rule)

settings <- expand.grid(i = ival, j = jval, bootstrap = "", tabularray = "")
if (is.null(i) && !is.null(j)) {
Expand Down Expand Up @@ -139,8 +184,9 @@ style_tt <- function (x,
}

if (!is.null(bootstrap_css) || !is.null(bootstrap_css_rule)) {
cal <- call("style_bootstrap", i = ival, j = jval, css = bootstrap_css, css_rule = bootstrap_css_rule)
out <- meta(out, "lazy_style", c(meta(out)$lazy_style, list(cal)))
out <- style_bootstrap(
i = ival, j = jval,
css = bootstrap_css, css_rule = bootstrap_css_rule)
}

return(out)
Expand Down
5 changes: 3 additions & 2 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,12 @@ meta <- function(x, get, set) {
eval_style <- function(x) {
out <- x

ls <- meta(x)$lazy_style
ls <- meta(out)$lazy_style

for (lazy_style in meta(x)$lazy_style) {
for (lazy_style in meta(out)$lazy_style) {
lazy_style[["x"]] <- out
out <- eval(lazy_style)
}

return(out)
}
24 changes: 24 additions & 0 deletions inst/tinytest/_tinysnapshot/latex-group_style_order.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

\begin{table}
\centering
\begin{tblr}[ %% tabularray outer open
] %% tabularray outer close
{ %% tabularray inner open
colspec={Q[]Q[]Q[]Q[]},
row{3}={,bg=black,fg=orange,},
row{4}={,bg=black,fg=orange,},
row{5}={,bg=black,fg=orange,},
row{6}={,bg=black,fg=orange,},
cell{1}{1}={c=2,}{halign=c,},
cell{1}{3}={c=2,}{halign=c,},
} %% tabularray inner close
\toprule
blah & & bar & \\ \cmidrule[lr]{1-2}\cmidrule[lr]{3-4}
mpg & cyl & disp & hp \\ \midrule %% TinyTableHeader
21 & 6 & 160 & 110 \\
21 & 6 & 160 & 110 \\
22.8 & 4 & 108 & 93 \\
21.4 & 6 & 258 & 110 \\
\bottomrule
\end{tblr}
\end{table}
11 changes: 11 additions & 0 deletions inst/tinytest/test-latex.R
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,14 @@ expect_snapshot_print(
expect_snapshot_print(
tt(x, output = "latex") |> style_tt(i = 1:2, j = 1:4, color = "orange"),
label = "latex-cell_color")


# Lazy style: group after style is respected
a <- tt(mtcars[1:4, 1:4], "latex") |>
style_tt(color = "orange", background = "black") |>
group_tt(j = list("blah" = 1:2, "bar" = 3:4))
b <- tt(mtcars[1:4, 1:4], "latex") |>
group_tt(j = list("blah" = 1:2, "bar" = 3:4)) |>
style_tt(color = "orange", background = "black")
expect_snapshot_print(a, label = "latex-group_style_order")
expect_equal(as.character(a), as.character(b))

0 comments on commit 540286d

Please sign in to comment.