Skip to content

Commit 8cfaf57

Browse files
test rendering 7 plot/table packages
these are just smoke tests but should fail if package apis change in incompatible ways adds about 20s to test time on this M3
1 parent 48410ea commit 8cfaf57

File tree

12 files changed

+503
-1
lines changed

12 files changed

+503
-1
lines changed

R/theme.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ theme_brand_ggplot <- function(brand_yml) {
8181
#' @param bg The background color
8282
#' @param fg The foreground color
8383
#'
84-
#' @theme_colors_export
84+
#' @export
8585
gt <- function(bg, fg) {
8686
(function(table) {
8787
table |>

tests/testthat/test-theme.R

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
2+
test_that("render flextable", {
3+
skip_if_no_quarto()
4+
quarto_render("theme/flextable.qmd", quiet = TRUE)
5+
expect_true(file.exists("theme/flextable.html"))
6+
unlink("theme/flextable.html")
7+
})
8+
9+
10+
test_that("render ggiraph", {
11+
skip_if_no_quarto()
12+
quarto_render("theme/ggiraph.qmd", quiet = TRUE)
13+
expect_true(file.exists("theme/ggiraph.html"))
14+
unlink("theme/ggiraph.html")
15+
})
16+
17+
18+
test_that("render ggplot", {
19+
skip_if_no_quarto()
20+
quarto_render("theme/ggplot.qmd", quiet = TRUE)
21+
expect_true(file.exists("theme/ggplot.html"))
22+
unlink("theme/ggplot.html")
23+
})
24+
25+
26+
test_that("render gt", {
27+
skip_if_no_quarto()
28+
quarto_render("theme/gt.qmd", quiet = FALSE)
29+
expect_true(file.exists("theme/gt.html"))
30+
unlink("theme/gt.html")
31+
})
32+
33+
34+
test_that("render heatmaply", {
35+
skip_if_no_quarto()
36+
quarto_render("theme/heatmaply.qmd", quiet = TRUE)
37+
expect_true(file.exists("theme/heatmaply.html"))
38+
unlink("theme/heatmaply.html")
39+
})
40+
41+
42+
test_that("render plotly-r", {
43+
skip_if_no_quarto()
44+
quarto_render("theme/plotly-r.qmd", quiet = TRUE)
45+
expect_true(file.exists("theme/plotly-r.html"))
46+
unlink("theme/plotly-r.html")
47+
})
48+
49+
50+
test_that("render thematic", {
51+
skip_if_no_quarto()
52+
quarto_render("theme/thematic.qmd", quiet = TRUE)
53+
expect_true(file.exists("theme/thematic.html"))
54+
unlink("theme/thematic.html")
55+
})
56+

tests/testthat/theme/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/.quarto/

tests/testthat/theme/flextable.qmd

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
---
2+
title: "knitr dark mode - flextable"
3+
brand:
4+
light: united-brand.yml
5+
dark: slate-brand.yml
6+
execute:
7+
echo: false
8+
warning: false
9+
---
10+
11+
```{r}
12+
#| echo: false
13+
#| warning: false
14+
library(flextable)
15+
library(quarto)
16+
17+
united_theme <- theme_brand_flextable('united-brand.yml')
18+
slate_theme <- theme_brand_flextable('slate-brand.yml')
19+
```
20+
21+
```{r}
22+
#| renderings: [light, dark]
23+
24+
ft <- flextable(airquality[ sample.int(10),])
25+
ft <- add_header_row(ft,
26+
colwidths = c(4, 2),
27+
values = c("Air quality", "Time")
28+
)
29+
ft <- theme_vanilla(ft)
30+
ft <- add_footer_lines(ft, "Daily air quality measurements in New York, May to September 1973.")
31+
ft <- color(ft, part = "footer", color = "#666666")
32+
ft <- set_caption(ft, caption = "New York Air Quality Measurements")
33+
34+
ft |> united_theme()
35+
ft |> slate_theme()
36+
```
37+
38+
Here's a [link](https://example.com).
39+
40+
{{< lipsum 2 >}}

tests/testthat/theme/ggiraph.qmd

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
---
2+
title: "dark mode - ggiraph"
3+
brand:
4+
light: united-brand.yml
5+
dark: slate-brand.yml
6+
---
7+
8+
```{r}
9+
#| echo: false
10+
#| warning: false
11+
library(quarto)
12+
library(ggplot2)
13+
library(ggiraph)
14+
15+
united_theme <- theme_brand_ggplot("united-brand.yml")
16+
slate_theme <- theme_brand_ggplot("slate-brand.yml")
17+
```
18+
19+
```{r}
20+
#| renderings: [light, dark]
21+
cars <- ggplot(mtcars, aes(mpg, wt)) +
22+
geom_point_interactive(aes(colour = factor(cyl), tooltip = rownames(mtcars))) +
23+
scale_colour_manual(values = c("darkorange", "purple", "cyan4"))
24+
25+
girafe(ggobj = cars + united_theme)
26+
girafe(ggobj = cars + slate_theme)
27+
```

tests/testthat/theme/ggplot.qmd

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
---
2+
title: "knitr dark mode - ggplot"
3+
brand:
4+
light: united-brand.yml
5+
dark: slate-brand.yml
6+
---
7+
``
8+
9+
```{r}
10+
#| echo: false
11+
#| warning: false
12+
library(quarto)
13+
library(ggplot2)
14+
15+
united_theme <- theme_brand_ggplot("united-brand.yml")
16+
slate_theme <- theme_brand_ggplot("slate-brand.yml")
17+
```
18+
19+
```{r}
20+
#| renderings: [light, dark]
21+
cars <- ggplot(mtcars, aes(mpg, wt)) +
22+
geom_point(aes(colour = factor(cyl))) +
23+
scale_colour_manual(values = c("darkorange", "purple", "cyan4"))
24+
25+
cars + united_theme
26+
cars + slate_theme
27+
```

tests/testthat/theme/gt.qmd

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
---
2+
title: "knitr dark mode - gt"
3+
brand:
4+
light: united-brand.yml
5+
dark: slate-brand.yml
6+
execute:
7+
echo: false
8+
warning: false
9+
---
10+
11+
```{r}
12+
#| echo: false
13+
#| warning: false
14+
library(gt)
15+
library(quarto)
16+
17+
united_theme <- theme_brand_gt('united-brand.yml')
18+
slate_theme <- theme_brand_gt('slate-brand.yml')
19+
```
20+
21+
```{r}
22+
#| renderings: [light, dark]
23+
library(tidyverse)
24+
penguins <- palmerpenguins::penguins |> filter(!is.na(sex))
25+
penguin_counts <- penguins |>
26+
mutate(year = as.character(year)) |>
27+
group_by(species, island, sex, year) |>
28+
summarise(n = n(), .groups = 'drop')
29+
penguin_counts_wider <- penguin_counts |>
30+
pivot_wider(
31+
names_from = c(species, sex),
32+
values_from = n
33+
) |>
34+
# Make missing numbers (NAs) into zero
35+
mutate(across(.cols = -(1:2), .fns = ~replace_na(., replace = 0))) |>
36+
arrange(island, year)
37+
38+
actual_colnames <- colnames(penguin_counts_wider)
39+
desired_colnames <- actual_colnames |>
40+
str_remove('(Adelie|Gentoo|Chinstrap)_') |>
41+
str_to_title()
42+
names(desired_colnames) <- actual_colnames
43+
44+
45+
penguins_table <- penguin_counts_wider |>
46+
mutate(across(.cols = -(1:2), ~if_else(. == 0, NA_integer_, .))) |>
47+
mutate(
48+
island = as.character(island),
49+
year = as.numeric(year),
50+
island = paste0('Island: ', island)
51+
) |>
52+
gt(groupname_col = 'island', rowname_col = 'year') |>
53+
cols_label(.list = desired_colnames) |>
54+
tab_spanner(
55+
label = md('**Adelie**'),
56+
columns = 3:4
57+
) |>
58+
tab_spanner(
59+
label = md('**Chinstrap**'),
60+
columns = c('Chinstrap_female', 'Chinstrap_male')
61+
) |>
62+
tab_spanner(
63+
label = md('**Gentoo**'),
64+
columns = contains('Gentoo')
65+
) |>
66+
tab_header(
67+
title = 'Penguins in the Palmer Archipelago',
68+
subtitle = 'Data is courtesy of the {palmerpenguins} R package'
69+
) |>
70+
sub_missing(missing_text = '-') |>
71+
summary_rows(
72+
groups = TRUE,
73+
fns = list(
74+
'Maximum' = ~max(.),
75+
'Total' = ~sum(.)
76+
),
77+
formatter = fmt_number,
78+
decimals = 0,
79+
missing_text = '-'
80+
) |>
81+
tab_options(
82+
data_row.padding = px(2),
83+
summary_row.padding = px(3), # A bit more padding for summaries
84+
row_group.padding = px(4) # And even more for our groups
85+
) |>
86+
opt_stylize(style = 6, color = 'gray')
87+
88+
penguins_table |> united_theme()
89+
90+
penguins_table |> slate_theme()
91+
```
92+
93+
Here's a [link](https://example.com).
94+
95+
{{< lipsum 3 >}}

tests/testthat/theme/heatmaply.qmd

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
---
2+
title: "dark mode - heatmaply"
3+
brand:
4+
light: united-brand.yml
5+
dark: slate-brand.yml
6+
---
7+
8+
```{r}
9+
#| echo: false
10+
#| warning: false
11+
library(heatmaply)
12+
library(quarto)
13+
14+
united_theme <- theme_brand_plotly('united-brand.yml')
15+
slate_theme <- theme_brand_plotly('slate-brand.yml')
16+
```
17+
18+
19+
```{r}
20+
#| warning: false
21+
#| renderings: [light, dark]
22+
23+
fig <- heatmaply(mtcars, k_row = 3, k_col = 2)
24+
25+
fig |> united_theme()
26+
fig |> slate_theme()
27+
```

tests/testthat/theme/plotly-r.qmd

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
---
2+
title: "knitr dark mode - plotly for R"
3+
brand:
4+
light: united-brand.yml
5+
dark: slate-brand.yml
6+
execute:
7+
echo: false
8+
warning: false
9+
---
10+
11+
```{r}
12+
#| echo: false
13+
#| warning: false
14+
library(quarto)
15+
library(plotly)
16+
17+
united_theme <- theme_brand_plotly('united-brand.yml')
18+
slate_theme <- theme_brand_plotly('slate-brand.yml')
19+
```
20+
21+
### no caption or crossref
22+
23+
```{r}
24+
#| renderings: [light, dark]
25+
fig <- plot_ly(iris, x = ~Species, y = ~Sepal.Width, type = 'violin',
26+
box = list(visible = TRUE),
27+
meanline = list(visible = TRUE),
28+
points = 'all')
29+
30+
fig |> united_theme()
31+
32+
fig |> slate_theme()
33+
```
34+
35+
36+
### crossref, no caption
37+
38+
::: {#fig-plotly-sepal-length}
39+
40+
```{r}
41+
#| renderings: [light, dark]
42+
43+
fig <- plot_ly(iris, x = ~Species, y = ~Sepal.Length, type = 'violin',
44+
box = list(visible = TRUE),
45+
meanline = list(visible = TRUE),
46+
points = 'all')
47+
48+
fig |> united_theme()
49+
50+
fig |> slate_theme()
51+
```
52+
53+
:::
54+
55+
### caption, no crossref
56+
57+
::: {}
58+
```{r}
59+
#| renderings: [light, dark]
60+
61+
# Load the iris dataset
62+
data("iris")
63+
64+
fig <- plot_ly(iris, x = ~Species, y = ~Petal.Width, type = 'violin',
65+
box = list(visible = TRUE),
66+
meanline = list(visible = TRUE),
67+
points = 'all')
68+
69+
fig |> united_theme()
70+
71+
fig |> slate_theme()
72+
```
73+
74+
Violin plot of Petal Width by Species
75+
76+
:::
77+
78+
79+
### caption and crossref
80+
81+
::: {#fig-plotly-petal-length}
82+
```{r}
83+
#| renderings: [light, dark]
84+
85+
# Load the iris dataset
86+
data("iris")
87+
88+
fig <- plot_ly(iris, x = ~Species, y = ~Petal.Length, type = 'violin',
89+
box = list(visible = TRUE),
90+
meanline = list(visible = TRUE),
91+
points = 'all')
92+
93+
fig |> united_theme()
94+
95+
fig |> slate_theme()
96+
```
97+
98+
Violin plot of Petal Length by Species
99+
100+
:::

0 commit comments

Comments
 (0)