Skip to content

Commit 36910e6

Browse files
ask knitr to hold/coalesce echos when renderings is set
fixes #12362 cc @cderv
1 parent f7076fe commit 36910e6

File tree

4 files changed

+85
-11
lines changed

4 files changed

+85
-11
lines changed

src/resources/rmd/hooks.R

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,11 @@ knitr_hooks <- function(format, resourceDir, handledLanguages) {
124124
options
125125
}
126126

127+
opts_hooks[["renderings"]] <- function(options){
128+
options$fig.show = 'hold'
129+
options
130+
}
131+
127132
opts_hooks[["collapse"]] <- function(options) {
128133
if (isTRUE(options[["collapse"]])) {
129134
comment <- options[["comment"]]

tests/docs/smoke-all/dark-mode/ggplot-brandless.qmd

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ ggplot(mtcars, aes(mpg, wt)) +
6363

6464
### with crossref but no caption
6565

66-
::: {#fig-thematic-ggplot}
66+
::: {#fig-ggplot}
6767
```{r}
6868
#| echo: true
6969
#| renderings:
@@ -89,15 +89,13 @@ ggplot(mtcars, aes(mpg, disp)) +
8989
geom_point(aes(colour = factor(cyl))) + colour_scale
9090
```
9191

92-
thematic - base r graphics
92+
ggplot - dark only
9393

9494
</div>
9595

96-
## patchwork
97-
9896
### with crossref and caption
9997

100-
::: {#fig-thematic-patchwork}
98+
::: {#fig-ggplot-mpg-hp}
10199
```{r}
102100
#| renderings: [light, dark]
103101
theme_set(united_theme)
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
---
2+
title: "knitr dark mode - ggplot"
3+
brand:
4+
light: united-brand.yml
5+
dark: slate-brand.yml
6+
execute:
7+
echo: false
8+
warning: false
9+
_quarto:
10+
tests:
11+
html:
12+
ensureHtmlElements:
13+
-
14+
- 'body.quarto-light'
15+
- 'div.cell div.light-content'
16+
- 'div.cell div.dark-content'
17+
- 'div.cell div.cell-code pre.code-with-copy'
18+
- []
19+
ensureFileRegexMatches:
20+
-
21+
- 'united_theme <span class="sc">\+</span> colour_scale'
22+
- 'slate_theme <span class="sc">\+</span> colour_scale'
23+
- []
24+
---
25+
26+
```{r}
27+
#| echo: false
28+
#| warning: false
29+
library(ggplot2)
30+
31+
ggplot_theme <- function(bgcolor, fgcolor) {
32+
theme_minimal(base_size = 11) %+%
33+
theme(
34+
panel.border = element_blank(),
35+
panel.grid.major.y = element_blank(),
36+
panel.grid.minor.y = element_blank(),
37+
panel.grid.major.x = element_blank(),
38+
panel.grid.minor.x = element_blank(),
39+
text = element_text(colour = fgcolor),
40+
axis.text = element_text(colour = fgcolor),
41+
rect = element_rect(colour = bgcolor, fill = bgcolor),
42+
plot.background = element_rect(fill = bgcolor, colour = NA),
43+
axis.line = element_line(colour = fgcolor),
44+
axis.ticks = element_line(colour = fgcolor)
45+
)
46+
}
47+
48+
brand_ggplot <- function(brand_yml) {
49+
brand <- yaml::yaml.load_file(brand_yml)
50+
ggplot_theme(brand$color$background, brand$color$foreground)
51+
}
52+
53+
united_theme <- brand_ggplot("united-brand.yml")
54+
slate_theme <- brand_ggplot("slate-brand.yml")
55+
56+
colour_scale <- scale_colour_manual(values = c("darkorange", "purple", "cyan4"))
57+
```
58+
59+
### with crossref but no caption
60+
61+
and `echo: true`
62+
63+
::: {#fig-ggplot}
64+
65+
```{r}
66+
#| echo: true
67+
#| renderings: [light, dark]
68+
ggplot(mtcars, aes(mpg, disp)) +
69+
geom_point(aes(colour = factor(cyl))) + united_theme + colour_scale
70+
ggplot(mtcars, aes(mpg, disp)) +
71+
geom_point(aes(colour = factor(cyl))) + slate_theme + colour_scale
72+
```
73+
74+
:::

tests/docs/smoke-all/dark-mode/ggplot-duobrand.qmd

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ ggplot(mtcars, aes(mpg, wt)) +
6565

6666
and `echo: true`
6767

68-
::: {#fig-thematic-ggplot}
68+
::: {#fig-ggplot}
6969

7070
```{r}
7171
#| echo: true
@@ -83,8 +83,6 @@ ggplot(mtcars, aes(mpg, disp)) +
8383

8484
### with caption but no crossref
8585

86-
dark rendering only
87-
8886
<div>
8987

9088
```{r}
@@ -93,15 +91,14 @@ ggplot(mtcars, aes(mpg, disp)) +
9391
geom_point(aes(colour = factor(cyl))) + slate_theme + colour_scale
9492
```
9593

96-
thematic - base r graphics
94+
ggplot - dark only
9795

9896
</div>
9997

100-
## patchwork
10198

10299
### with crossref and caption
103100

104-
::: {#fig-thematic-patchwork}
101+
::: {#fig-ggplot-mpg-hp}
105102
```{r}
106103
#| renderings: [light, dark]
107104
ggplot(mtcars, aes(mpg, hp)) +

0 commit comments

Comments
 (0)