Skip to content

Commit

Permalink
snapshots
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentarelbundock committed Jan 18, 2024
1 parent b9737b2 commit d3e4fc7
Show file tree
Hide file tree
Showing 21 changed files with 105 additions and 98 deletions.
80 changes: 42 additions & 38 deletions R/format_tt.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
#' num_mark_big = " ")
#' tt(tab)
#'
format_tt <- function(x = NULL,
format_tt <- function(x,
j = NULL,
output = NULL,
digits = getOption("digits"),
Expand All @@ -52,54 +52,59 @@ format_tt <- function(x = NULL,
) {

out <- x
cal <- call("format_tt_lazy",
j = j,
output = output,
digits = digits,
num_fmt = num_fmt,
num_zero = num_zero,
num_suffix = num_suffix,
num_mark_big = num_mark_big,
num_mark_dec = num_mark_dec,
sprintf = sprintf,
url = url,
date = date,
bool = bool,
other = other)

if (inherits(out, "tinytable")) {
cal <- call("format_tt_lazy",
j = j,
output = output,
digits = digits,
num_fmt = num_fmt,
num_zero = num_zero,
num_suffix = num_suffix,
num_mark_big = num_mark_big,
num_mark_dec = num_mark_dec,
sprintf = sprintf,
url = url,
date = date,
bool = bool,
other = other)
out <- meta(out, "lazy_format", c(meta(out)$lazy_format, list(cal)))

# this should be evaluated immediately for data frames and vectors
} else {
cal[["x"]] <- x
out <- eval(out)
}

out <- format_tt_lazy(out,
j = j,
output = output,
digits = digits,
num_fmt = num_fmt,
num_zero = num_zero,
num_suffix = num_suffix,
num_mark_big = num_mark_big,
num_mark_dec = num_mark_dec,
sprintf = sprintf,
url = url,
date = date,
bool = bool,
other = other)
}
return(out)
}

format_tt_lazy <- function(x,
j,
output,
j = NULL,
output = NULL ,
digits,
num_fmt,
num_zero,
num_suffix,
num_mark_big,
num_mark_dec,
sprintf,
url,
date,
bool,
other
num_fmt = "significant",
num_zero = FALSE,
num_suffix = FALSE,
num_mark_big = "",
num_mark_dec = NULL,
sprintf = NULL,
url = FALSE,
date = "%Y-%m-%d",
bool = identity,
other = identity
) {


if (inherits(x, "tinytable")) {
msg <- "`format_tt()` must be called *before* `tt()`. You must format your dataset before drawing a table."
}

if (isTRUE(check_atomic_vector(x))) {
atomic_vector <- TRUE
x <- data.frame(tinytable = x)
Expand Down Expand Up @@ -138,7 +143,6 @@ format_tt_lazy <- function(x,

# format each column
for (col in j) {

# sprintf() is self-contained
if (!is.null(sprintf)) {
x[[col]] <- base::sprintf(sprintf, x[[col]])
Expand Down
12 changes: 7 additions & 5 deletions R/tt.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
#' @export
tt <- function(x,
output = NULL,
digits = getOption("digits"),
digits = NULL,
caption = NULL,
width = NULL,
notes = NULL,
Expand All @@ -49,10 +49,6 @@ tt <- function(x,
assert_numeric(width, len = 1, lower = 0, upper = 1, null.ok = TRUE)
assert_integerish(digits, len = 1, null.ok = TRUE)

# formatting options are limited here
if (!is.null(digits)) {
x <- format_tt(x, digits = digits)
}

# notes can be a single string or a (named) list of strings
if (is.character(notes) && length(notes)) {
Expand Down Expand Up @@ -86,5 +82,11 @@ tt <- function(x,

out <- meta(out, "lazy_tt", cal)

# formatting options are limited here
# after creating the table since the new lazy system
if (!is.null(digits)) {
out <- format_tt(out, digits = digits)
}

return(out)
}
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@


<p align="center">
<img src="man/figures/tinytable_logo.svg" height = "250" class = "center">
</p>
Expand Down
Binary file modified inst/tinytest/_tinysnapshot/html-striped.rds
Binary file not shown.
Binary file modified inst/tinytest/_tinysnapshot/html-striped_orange.rds
Binary file not shown.
Binary file modified inst/tinytest/_tinysnapshot/html-vectorized_color_j.rds
Binary file not shown.
6 changes: 3 additions & 3 deletions inst/tinytest/_tinysnapshot/latex-align.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ column{5}={halign=r,},
} %% tabularray inner close
\toprule
mpg & cyl & disp & hp & drat \\ \midrule %% TinyTableHeader
21.0 & 6 & 160 & 110 & 3.90 \\
21.0 & 6 & 160 & 110 & 3.90 \\
22.8 & 4 & 108 & 93 & 3.85 \\
21 & 6 & 160 & 110 & 3.9 \\
21 & 6 & 160 & 110 & 3.9 \\
22.8 & 4 & 108 & 93 & 3.85 \\
21.4 & 6 & 258 & 110 & 3.08 \\
\bottomrule
\end{tblr}
Expand Down
6 changes: 3 additions & 3 deletions inst/tinytest/_tinysnapshot/latex-cell_color.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ cell{3}{4}={}{,fg=orange,},
} %% tabularray inner close
\toprule
mpg & cyl & disp & hp & drat \\ \midrule %% TinyTableHeader
21.0 & 6 & 160 & 110 & 3.90 \\
21.0 & 6 & 160 & 110 & 3.90 \\
22.8 & 4 & 108 & 93 & 3.85 \\
21 & 6 & 160 & 110 & 3.9 \\
21 & 6 & 160 & 110 & 3.9 \\
22.8 & 4 & 108 & 93 & 3.85 \\
21.4 & 6 & 258 & 110 & 3.08 \\
\bottomrule
\end{tblr}
Expand Down
6 changes: 3 additions & 3 deletions inst/tinytest/_tinysnapshot/latex-col_color.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ column{4}={,fg=orange,},
} %% tabularray inner close
\toprule
mpg & cyl & disp & hp & drat \\ \midrule %% TinyTableHeader
21.0 & 6 & 160 & 110 & 3.90 \\
21.0 & 6 & 160 & 110 & 3.90 \\
22.8 & 4 & 108 & 93 & 3.85 \\
21 & 6 & 160 & 110 & 3.9 \\
21 & 6 & 160 & 110 & 3.9 \\
22.8 & 4 & 108 & 93 & 3.85 \\
21.4 & 6 & 258 & 110 & 3.08 \\
\bottomrule
\end{tblr}
Expand Down
7 changes: 3 additions & 4 deletions inst/tinytest/_tinysnapshot/latex-default.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@
] %% tabularray outer close
{ %% tabularray inner open
colspec={Q[]Q[]Q[]Q[]Q[]},
row{even}={bg=black!5!white},
} %% tabularray inner close
\toprule
mpg & cyl & disp & hp & drat \\ \midrule %% TinyTableHeader
21.0 & 6 & 160 & 110 & 3.90 \\
21.0 & 6 & 160 & 110 & 3.90 \\
22.8 & 4 & 108 & 93 & 3.85 \\
21 & 6 & 160 & 110 & 3.9 \\
21 & 6 & 160 & 110 & 3.9 \\
22.8 & 4 & 108 & 93 & 3.85 \\
21.4 & 6 & 258 & 110 & 3.08 \\
\bottomrule
\end{tblr}
Expand Down
10 changes: 5 additions & 5 deletions inst/tinytest/_tinysnapshot/latex-group_style_order.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@
] %% tabularray outer close
{ %% tabularray inner open
colspec={Q[]Q[]Q[]Q[]},
cell{1}{1}={c=2,}{halign=c,},
cell{1}{3}={c=2,}{halign=c,},
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.0 & 6 & 160 & 110 \\
21.0 & 6 & 160 & 110 \\
22.8 & 4 & 108 & 93 \\
21 & 6 & 160 & 110 \\
21 & 6 & 160 & 110 \\
22.8 & 4 & 108 & 93 \\
21.4 & 6 & 258 & 110 \\
\bottomrule
\end{tblr}
Expand Down
6 changes: 3 additions & 3 deletions inst/tinytest/_tinysnapshot/latex-nohead.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
colspec={Q[]Q[]Q[]Q[]Q[]},
} %% tabularray inner close
\toprule
21.0 & 6 & 160 & 110 & 3.90 \\
21.0 & 6 & 160 & 110 & 3.90 \\
22.8 & 4 & 108 & 93 & 3.85 \\
21 & 6 & 160 & 110 & 3.9 \\
21 & 6 & 160 & 110 & 3.9 \\
22.8 & 4 & 108 & 93 & 3.85 \\
21.4 & 6 & 258 & 110 & 3.08 \\
\bottomrule
\end{tblr}
Expand Down
6 changes: 3 additions & 3 deletions inst/tinytest/_tinysnapshot/latex-row_color.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ row{5}={,fg=orange,},
} %% tabularray inner close
\toprule
mpg & cyl & disp & hp & drat \\ \midrule %% TinyTableHeader
21.0 & 6 & 160 & 110 & 3.90 \\
21.0 & 6 & 160 & 110 & 3.90 \\
22.8 & 4 & 108 & 93 & 3.85 \\
21 & 6 & 160 & 110 & 3.9 \\
21 & 6 & 160 & 110 & 3.9 \\
22.8 & 4 & 108 & 93 & 3.85 \\
21.4 & 6 & 258 & 110 & 3.08 \\
\bottomrule
\end{tblr}
Expand Down
6 changes: 3 additions & 3 deletions inst/tinytest/_tinysnapshot/latex-theme_grid.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ colspec={Q[]Q[]Q[]Q[]Q[]},
hlines={},vlines={},
} %% tabularray inner close
mpg & cyl & disp & hp & drat \\
21.0 & 6 & 160 & 110 & 3.90 \\
21.0 & 6 & 160 & 110 & 3.90 \\
22.8 & 4 & 108 & 93 & 3.85 \\
21 & 6 & 160 & 110 & 3.9 \\
21 & 6 & 160 & 110 & 3.9 \\
22.8 & 4 & 108 & 93 & 3.85 \\
21.4 & 6 & 258 & 110 & 3.08 \\
\end{tblr}
\end{table}
6 changes: 3 additions & 3 deletions inst/tinytest/_tinysnapshot/latex-theme_striped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ row{even}={bg=black!5!white},
} %% tabularray inner close
\toprule
mpg & cyl & disp & hp & drat \\ \midrule %% TinyTableHeader
21.0 & 6 & 160 & 110 & 3.90 \\
21.0 & 6 & 160 & 110 & 3.90 \\
22.8 & 4 & 108 & 93 & 3.85 \\
21 & 6 & 160 & 110 & 3.9 \\
21 & 6 & 160 & 110 & 3.9 \\
22.8 & 4 & 108 & 93 & 3.85 \\
21.4 & 6 & 258 & 110 & 3.08 \\
\bottomrule
\end{tblr}
Expand Down
6 changes: 3 additions & 3 deletions inst/tinytest/_tinysnapshot/latex-theme_void.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
colspec={Q[]Q[]Q[]Q[]Q[]},
} %% tabularray inner close
mpg & cyl & disp & hp & drat \\
21.0 & 6 & 160 & 110 & 3.90 \\
21.0 & 6 & 160 & 110 & 3.90 \\
22.8 & 4 & 108 & 93 & 3.85 \\
21 & 6 & 160 & 110 & 3.9 \\
21 & 6 & 160 & 110 & 3.9 \\
22.8 & 4 & 108 & 93 & 3.85 \\
21.4 & 6 & 258 & 110 & 3.08 \\
\end{tblr}
\end{table}
6 changes: 3 additions & 3 deletions inst/tinytest/_tinysnapshot/markdown-nocolnames.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@

| 5.1 | 3.5 | 1.4 | 0.2 | setosa |
| 4.9 | 3.0 | 1.4 | 0.2 | setosa |
| 4.9 | 3 | 1.4 | 0.2 | setosa |
| 4.7 | 3.2 | 1.3 | 0.2 | setosa |
| 4.6 | 3.1 | 1.5 | 0.2 | setosa |
| 5.0 | 3.6 | 1.4 | 0.2 | setosa |
| 5 | 3.6 | 1.4 | 0.2 | setosa |
| 5.4 | 3.9 | 1.7 | 0.4 | setosa |
| 4.6 | 3.4 | 1.4 | 0.3 | setosa |
| 5.0 | 3.4 | 1.5 | 0.2 | setosa |
| 5 | 3.4 | 1.5 | 0.2 | setosa |
| 4.4 | 2.9 | 1.4 | 0.2 | setosa |
| 4.9 | 3.1 | 1.5 | 0.1 | setosa |
2 changes: 1 addition & 1 deletion inst/tinytest/test-latex.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ using("tinysnapshot")
x <- mtcars[1:4, 1:5]

expect_snapshot_print(
tt(x, output = "latex", theme = "striped"),
tt(x, output = "latex"),
label = "latex-default")

k <- x
Expand Down
2 changes: 1 addition & 1 deletion man/format_tt.Rd

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

2 changes: 1 addition & 1 deletion man/tt.Rd

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

Loading

0 comments on commit d3e4fc7

Please sign in to comment.