Skip to content

Commit 151b43b

Browse files
committed
Add Quarto vignettes + interactive vignette.
1 parent 42a6588 commit 151b43b

File tree

10 files changed

+332
-178
lines changed

10 files changed

+332
-178
lines changed

.Rbuildignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
^LICENSE\.md$
1414
^CITATION\.cff$
1515
vignettes
16+
^vignettes/\.quarto$
17+
pkgdown/assets/gt-latex.qmd
1618
man/figures/
1719
tests/gt-examples
1820
tests/performance-monitoring

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,4 @@ docs
4646
*.qmd
4747
/*.docx
4848
tests/testthat/Rplots.pdf
49+
!pkgdown/assets/*.pdf

DESCRIPTION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ Suggests:
7373
webshot2 (>= 0.1.0),
7474
withr
7575
Config/Needs/coverage: officer
76+
Config/Needs/website: quarto
7677
ByteCompile: true
7778
Config/testthat/edition: 3
7879
Config/testthat/parallel: true

pkgdown/_pkgdown.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,5 +385,25 @@ reference:
385385
- rx_adsl
386386
- rx_addv
387387

388+
389+
articles:
390+
- title: Get started
391+
navbar: ~
392+
contents:
393+
- creating-summary-lines
394+
- title: Case studies
395+
navbar: Case studies
396+
contents:
397+
- case-study-gtcars
398+
- case-study-clinical-tables
399+
400+
- title: Datasets
401+
contents:
402+
- gt-datasets
403+
- title: Visual tests
404+
contents:
405+
- gt-interactive
406+
- gt-visual
407+
388408
redirects:
389409
- ["articles/intro-creating-gt-tables.html", "articles/gt.html"]

pkgdown/assets/gt-latex.pdf

-13 KB
Binary file not shown.

scripts/visual-tests-latex.R

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
header_latex <- c(
2+
"---", 'title: "LaTeX Quarto test"', "editor: source", "format:", " pdf:",
3+
" colorlinks: true ", " geometry:", " - top=10mm",
4+
" - left=10mm", " - bottom=10mm", " hyperrefoptions:",
5+
" - linktoc=all", "toc: false", "tbl-cap-location: bottom", "lot: true",
6+
"html-table-processing: none", "---", "",
7+
"<!--- This file is generated from sourcing scripts/visual-tests-latex.R You can add more tests to vignettes/gt-visual.qmd"
8+
)
9+
10+
strip_knitr_empty <- function(lines) {
11+
chunk_start_location <- grep("## ----", lines, fixed = TRUE)
12+
empty_locations <- !nzchar(lines)
13+
14+
remove <- vector(mode = "integer")
15+
16+
for (i in seq_along(chunk_start_location)) {
17+
if (empty_locations[chunk_start_location[i] + 1]) {
18+
remove <- c(remove, chunk_start_location[i], chunk_start_location[i] + 1)
19+
}
20+
}
21+
if (rlang::has_length(remove)) {
22+
if (anyNA(remove)) {
23+
cli::cli_abort("Internal error. remove shouldn't contain NA.")
24+
}
25+
lines <- lines[-remove]
26+
}
27+
lines
28+
}
29+
30+
get_example_metadata <- function(lines) {
31+
# Ensure consistent formatting
32+
chunk_labels <- stringr::str_subset(lines, "#\\| label\\:")
33+
n_chunks <- length(chunk_labels)
34+
35+
# having a table title tell us what to look for when comparing tests.
36+
table_titles <- stringr::str_subset(lines, "tab_header\\(title")
37+
38+
if (length(table_titles) != n_chunks) {
39+
cli::cli_abort(c(
40+
"The structure is not respected. We have {n_chunks} examples, but {length(table_titles)}.",
41+
"Each table should be labelled with #| label: and have a tab_header(title = \"\") title"
42+
))
43+
}
44+
45+
chunk_labels <- gsub(".+\\:\\s?(.+)", "\\1", chunk_labels)
46+
table_titles <- gsub(".*tab_header\\(title\\s?=\\s?\"(.+)\"\\).*", "\\1", table_titles)
47+
48+
list(
49+
label = chunk_labels,
50+
title = table_titles
51+
)
52+
53+
}
54+
55+
get_replacement_lines <- function(metadata) {
56+
length.out <- unique(lengths(metadata))
57+
if (length(length.out) != 1) {
58+
cli::cli_abort("issue")
59+
}
60+
new_lines <- vector("list", length = length.out)
61+
for (i in seq_along(new_lines)) {
62+
new_lines[[i]] <- c(
63+
paste0("#| label: tbl-", metadata$label[[i]]),
64+
paste0("#| tbl-cap: \"" , metadata$title[[i]], "\""),
65+
"#| echo: false",
66+
"tab"
67+
)
68+
}
69+
new_lines
70+
}
71+
72+
lines_html <- function() {
73+
74+
tmp <- withr::local_tempfile()
75+
# create output file
76+
knitr::purl("vignettes/gt-visual.qmd", output = tmp, quiet = TRUE)
77+
78+
lines <- readLines(tmp, encoding = "UTF-8", warn = FALSE)
79+
80+
lines <- stringr::str_subset(
81+
lines,
82+
"#\\| echo\\: false|opt_interactive|plot\\(tab\\)",
83+
negate = TRUE
84+
85+
)
86+
# find the first test (then assume to keep 3 extra lines)
87+
first_test_identifier <- grep("tab <- ", lines, fixed = TRUE)[1] - 3L
88+
lines <- lines[-seq_len(first_test_identifier)]
89+
90+
# remove empty chunks
91+
lines <- strip_knitr_empty(lines)
92+
lines
93+
}
94+
95+
lines_stripped <-lines_html()
96+
97+
replacement_lines <- get_example_metadata(lines_stripped) |> get_replacement_lines()
98+
99+
list_lines <- as.list(lines_stripped)
100+
n_replacements <- 1
101+
for (i in seq_along(list_lines)) {
102+
if (list_lines[[i]] == "tab") {
103+
list_lines[[i]] <- replacement_lines[[n_replacements]]
104+
n_replacements <- n_replacements + 1
105+
}
106+
}
107+
108+
final_lines <- unlist(list_lines)
109+
c(header_latex,
110+
final_lines
111+
) |>
112+
writeLines("pkgdown/assets/gt-latex.qmd")
113+
quarto::quarto_render("pkgdown/assets/gt-latex.qmd")
114+
unlink("pkgdown/assets/gt-latex.qmd")

vignettes/.gitignore

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
*.html
2-
*.R
3-
*.css
4-
!visual_tests.R
5-
!*.qmd
6-
7-
/.quarto/
1+
*.html
2+
*.R
3+
*.css
4+
!visual_tests.R
5+
!*.qmd
6+
*.html
7+
*.R
8+
*.log
9+
*_files
10+
11+
/.quarto/

vignettes/gt-interactive.qmd

Lines changed: 0 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@ gt_tbl |>
3535
opt_interactive()
3636
```
3737

38-
39-
4038
# Current limitations
4139

4240
* Some features like `tab_style()` may not be fully supported.
@@ -45,112 +43,3 @@ gt_tbl |>
4543

4644
* Your interactive table may be visually different from your non-interactive table.
4745

48-
49-
# Visual tests static vs interactive
50-
51-
# `math formulas()`
52-
53-
@tbl-md contains many things.
54-
55-
```{r}
56-
#| tbl-cap: "A table"
57-
#| label: tbl-md
58-
#| code-fold: true
59-
60-
df <- data.frame(
61-
idx = 1:37,
62-
l_time_domain =
63-
c(
64-
"$1$",
65-
"${{\\bf{e}}^{a\\,t}}$",
66-
"${t^n},\\,\\,\\,\\,\\,n = 1,2,3, \\ldots$",
67-
"${t^p}, p > -1$",
68-
"$\\sqrt t$",
69-
"${t^{n - \\frac{1}{2}}},\\,\\,\\,\\,\\,n = 1,2,3, \\ldots$",
70-
"$\\sin \\left( {at} \\right)$",
71-
"$\\cos \\left( {at} \\right)$",
72-
"$t\\sin \\left( {at} \\right)$",
73-
"$t\\cos \\left( {at} \\right)$",
74-
"$\\sin \\left( {at} \\right) - at\\cos \\left( {at} \\right)$",
75-
"$\\sin \\left( {at} \\right) + at\\cos \\left( {at} \\right)$",
76-
"$\\cos \\left( {at} \\right) - at\\sin \\left( {at} \\right)$",
77-
"$\\cos \\left( {at} \\right) + at\\sin \\left( {at} \\right)$",
78-
"$\\sin \\left( {at + b} \\right)$",
79-
"$\\cos \\left( {at + b} \\right)$",
80-
"$\\sinh \\left( {at} \\right)$",
81-
"$\\cosh \\left( {at} \\right)$",
82-
"${{\\bf{e}}^{at}}\\sin \\left( {bt} \\right)$",
83-
"${{\\bf{e}}^{at}}\\cos \\left( {bt} \\right)$",
84-
"${{\\bf{e}}^{at}}\\sinh \\left( {bt} \\right)$",
85-
"${{\\bf{e}}^{at}}\\cosh \\left( {bt} \\right)$",
86-
"${t^n}{{\\bf{e}}^{at}},\\,\\,\\,\\,\\,n = 1,2,3, \\ldots$",
87-
"$f\\left( {ct} \\right)$",
88-
"${u_c}\\left( t \\right) = u\\left( {t - c} \\right)$",
89-
"$\\delta \\left( {t - c} \\right)$",
90-
"${u_c}\\left( t \\right)f\\left( {t - c} \\right)$",
91-
"${u_c}\\left( t \\right)g\\left( t \\right)$",
92-
"${{\\bf{e}}^{ct}}f\\left( t \\right)$",
93-
"${t^n}f\\left( t \\right),\\,\\,\\,\\,\\,n = 1,2,3, \\ldots$",
94-
"$\\displaystyle \\frac{1}{t}f\\left( t \\right)$",
95-
"$\\displaystyle \\int_{{\\,0}}^{{\\,t}}{{\\,f\\left( v \\right)\\,dv}}$",
96-
"$\\displaystyle \\int_{{\\,0}}^{{\\,t}}{{f\\left( {t - \\tau } \\right)g\\left( \\tau \\right)\\,d\\tau }}$",
97-
"$f\\left( {t + T} \\right) = f\\left( t \\right)$",
98-
"$f'\\left( t \\right)$",
99-
"$f''\\left( t \\right)$",
100-
"${f^{\\left( n \\right)}}\\left( t \\right)$"
101-
),
102-
l_laplace_s_domain =
103-
c(
104-
"$$\\frac{1}{s}$$",
105-
"$$\\frac{1}{{s - a}}$$",
106-
"$$\\frac{{n!}}{{{s^{n + 1}}}}$$",
107-
"$$\\frac{{\\Gamma \\left( {p + 1} \\right)}}{{{s^{p + 1}}}}$$",
108-
"$$\\frac{{\\sqrt \\pi }}{{2{s^{\\frac{3}{2}}}}}$$",
109-
"$$\\frac{{1 \\cdot 3 \\cdot 5 \\cdots \\left( {2n - 1} \\right)\\sqrt \\pi }}{{{2^n}{s^{n + \\frac{1}{2}}}}}$$",
110-
"$$\\frac{a}{{{s^2} + {a^2}}}$$",
111-
"$$\\frac{s}{{{s^2} + {a^2}}}$$",
112-
"$$\\frac{{2as}}{{{{\\left( {{s^2} + {a^2}} \\right)}^2}}}$$",
113-
"$$\\frac{{{s^2} - {a^2}}}{{{{\\left( {{s^2} + {a^2}} \\right)}^2}}}$$",
114-
"$$\\frac{{2{a^3}}}{{{{\\left( {{s^2} + {a^2}} \\right)}^2}}}$$",
115-
"$$\\frac{{2a{s^2}}}{{{{\\left( {{s^2} + {a^2}} \\right)}^2}}}$$",
116-
"$$\\frac{{s\\left( {{s^2} - {a^2}} \\right)}}{{{{\\left( {{s^2} + {a^2}} \\right)}^2}}}$$",
117-
"$$\\frac{{s\\left( {{s^2} + 3{a^2}} \\right)}}{{{{\\left( {{s^2} + {a^2}} \\right)}^2}}}$$",
118-
"$$\\frac{{s\\sin \\left( b \\right) + a\\cos \\left( b \\right)}}{{{s^2} + {a^2}}}$$",
119-
"$$\\frac{{s\\cos \\left( b \\right) - a\\sin \\left( b \\right)}}{{{s^2} + {a^2}}}$$",
120-
"$$\\frac{a}{{{s^2} - {a^2}}}$$",
121-
"$$\\frac{s}{{{s^2} - {a^2}}}$$",
122-
"$$\\frac{b}{{{{\\left( {s - a} \\right)}^2} + {b^2}}}$$",
123-
"$$\\frac{{s - a}}{{{{\\left( {s - a} \\right)}^2} + {b^2}}}$$",
124-
"$$\\frac{b}{{{{\\left( {s - a} \\right)}^2} - {b^2}}}$$",
125-
"$$\\frac{{s - a}}{{{{\\left( {s - a} \\right)}^2} - {b^2}}}$$",
126-
"$$\\frac{{n!}}{{{{\\left( {s - a} \\right)}^{n + 1}}}}$$",
127-
"$$\\frac{1}{c}F\\left( {\\frac{s}{c}} \\right)$$",
128-
"$$\\frac{{{{\\bf{e}}^{ - cs}}}}{s}$$",
129-
"${{\\bf{e}}^{ - cs}}$",
130-
"${{\\bf{e}}^{ - cs}}F\\left( s \\right)$",
131-
"${{\\bf{e}}^{ - cs}}{\\mathcal{L}}\\left\\{ {g\\left( {t + c} \\right)} \\right\\}$",
132-
"$F\\left( {s - c} \\right)$",
133-
"${\\left( { - 1} \\right)^n}{F^{\\left( n \\right)}}\\left( s \\right)$",
134-
"$\\int_{{\\,s}}^{{\\,\\infty }}{{F\\left( u \\right)\\,du}}$",
135-
"$\\displaystyle \\frac{{F\\left( s \\right)}}{s}$",
136-
"$F\\left( s \\right)G\\left( s \\right)$",
137-
"$\\displaystyle \\frac{{\\displaystyle \\int_{{\\,0}}^{{\\,T}}{{{{\\bf{e}}^{ - st}}f\\left( t \\right)\\,dt}}}}{{1 - {{\\bf{e}}^{ - sT}}}}$",
138-
"$sF\\left( s \\right) - f\\left( 0 \\right)$",
139-
"${s^2}F\\left( s \\right) - sf\\left( 0 \\right) - f'\\left( 0 \\right)$",
140-
"${s^n}F\\left( s \\right) - {s^{n - 1}}f\\left( 0 \\right) - {s^{n - 2}}f'\\left( 0 \\right) \\cdots - s{f^{\\left( {n - 2} \\right)}}\\left( 0 \\right) - {f^{\\left( {n - 1} \\right)}}\\left( 0 \\right)$"
141-
)
142-
) |>
143-
gt() |>
144-
fmt_markdown() |>
145-
cols_label(
146-
idx = "",
147-
l_time_domain = md("$f\\left( t \\right) = {\\mathcal{L}^{\\,\\, - 1}}\\left\\{ {F\\left( s \\right)} \\right\\}$"),
148-
l_laplace_s_domain = md("$F\\left( s \\right) = \\mathcal{L}\\left\\{ {f\\left( t \\right)} \\right\\}$")
149-
) |>
150-
tab_header(title = "A Table of Laplace Transforms") |>
151-
tab_source_note(
152-
source_note = md("The hyperbolic functions: $\\cosh \\left( t \\right) = \\frac{{{{\\bf{e}}^t} + {{\\bf{e}}^{ - t}}}}{2}$ , $\\sinh \\left( t \\right) = \\frac{{{{\\bf{e}}^t} - {{\\bf{e}}^{ - t}}}}{2}$")
153-
) |>
154-
cols_align(align = "center")
155-
156-
```

0 commit comments

Comments
 (0)