Skip to content

Commit bdd6506

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

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
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"]]
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+
:::

0 commit comments

Comments
 (0)