-
Notifications
You must be signed in to change notification settings - Fork 301
Expand file tree
/
Copy paththeme.Rmd
More file actions
executable file
·272 lines (195 loc) · 9.46 KB
/
theme.Rmd
File metadata and controls
executable file
·272 lines (195 loc) · 9.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
---
title: "Theming flexdashboard"
resource_files:
- snippets
---
```{r setup, include=FALSE}
library(quillt)
```
## Bootstrap and bslib
The `flex_dashboard` output format builds upon the [Bootstrap CSS framework](https://getbootstrap.com/). Bootstrap is a library of reusable chunks of HTML, CSS, and JavaScript code that you can call on to modify your flexdashboard without writing that code yourself. Originally [created by and for Twitter](https://en.wikipedia.org/wiki/Bootstrap_(front-end_framework)), it is a free and open source project; you can view the repository on [GitHub](https://github.com/twbs/bootstrap).
The `flex_dashboard` output format includes a `theme` parameter for customizing the overall appearance of the dashboard. This parameter has special integration with the [`{bslib}` package](https://rstudio.github.io/bslib/), which makes it easy to customize [Bootstrap CSS](https://getbootstrap.com/) styles directly from R.
`{bslib}` [supports multiple versions](https://rstudio.github.io/bslib/articles/bslib.html#bootstrap-versions) of Bootstrap so that both new and legacy projects can leverage [custom themes](https://rstudio.github.io/bslib/articles/bslib.html#custom-themes). If you are starting on a fresh project, we recommend choosing the latest supported version (currently, `version: 4`), but legacy projects may want to set `version: 3`:
``` {.yaml}
---
output:
flexdashboard::flex_dashboard:
theme:
version: 4
---
```
### Bootswatch themes
`{bslib}` provides access to any [Bootswatch 4](https://bootswatch.com/) (new projects) or [3](https://bootswatch.com/3) (legacy projects). For example, here's how you could give your dashboard a [(Bootswatch 4) Minty theme](https://bootswatch.com/minty/).
``` {.yaml}
---
output:
flexdashboard::flex_dashboard:
theme:
version: 4
bootswatch: minty
---
```
This screenshot shows the result of applying the above `theme` to [this example dashboard](https://gist.github.com/cpsievert/04bedbe9af652fab8c24a5e5eee0d721) that [uses Shiny](shiny.html) (subsequent screenshots also use this example, but swap out different `theme`s).
{.snippet}
### Custom themes {#custom-themes}
You can also create your own [custom `{bslib}` theme](https://rstudio.github.io/bslib/articles/bslib.html#custom-themes) by setting the [main colors and fonts](https://rstudio.github.io/bslib/articles/bslib.html#main-colors-fonts) as well as [more specific theming](https://rstudio.github.io/bslib/articles/bslib.html#everything-else) options, [such as `navbar-bg`](https://rstudio.github.io/bslib/articles/bs4-variables.html#navbar-bg). For custom font settings, adding a `google:` keyword triggers `sass::font_google()`'s ability to automatically import Google Font files. By default, `google:` will bundle font files with your dashboard, so that when you share it with someone else, the fonts are guaranteed to render, even without an internet connection (`local: false` imports files via URL instead of serving them locally).
``` {.r}
---
output:
flexdashboard::flex_dashboard:
theme:
version: 4
bg: "#101010"
fg: "#FDF7F7"
primary: "#ED79F9"
navbar-bg: "#3ADAC6"
base_font:
google: Prompt
heading_font:
google: Sen
code_font:
google:
# arguments to sass::font_google()
family: JetBrains Mono
local: false
---
```
{.snippet}
[See here](https://rstudio.github.io/bslib/articles/bslib.html#custom-themes) to learn more about `{bslib}`'s custom themes.
## Real-time theming {#real-time-themes}
[`{bslib}`'s real-time theming widget](https://rstudio.github.io/bslib/articles/bslib.html#real-time-theming) allows you to interactively customize `theme:` by adding:
- `runtime: shiny` to the document's yaml matter and calling `bslib::bs_themer()` in any code chunk, or
- `runtime: shinyrmd` to the document's yaml matter and calling `bslib::bs_themer()` in a code chunk with `context='server'`:
```{r setup, include = FALSE, context = "server"}`r ''`
bslib::bs_themer()
```
{.snippet}
### Auto-theming R plots
Generally, most "un-opinionated" HTML content rendered by the web browser should "just work" with your `theme` settings. However, some outputs that aren't rendered by the browser (e.g., R plots) aren't necessarily aware of `{bslib}` (or CSS in general).
To theme static R plots (e.g., `{ggplot2}`), you can call `thematic::thematic_rmd()` in a setup chunk to translate the `{bslib}` theme main colors to R plot theming defaults.
Additionally, if you're using Google Fonts, or some other font that you've registered with R, you can safely include `font = "auto"` to translate fonts as well:
```{r setup, include = FALSE}`r ''`
thematic::thematic_rmd(font = "auto")
```
{.snippet}
To learn more about `{thematic}`, see <https://rstudio.github.io/thematic>
## Add CSS styles
Although [custom theming](#custom-themes) is great for customizing Bootstrap and Flexdashboard CSS, you still may want to add more styling rules to customize the appearance of your dashboard. This file can contain plain CSS rules, like:
``` {.css}
.my-purple {
background-color: #da70d6;
}
```
For this, you can provide relevant file(s) to `flex_dashboard`'s `css` argument:
``` {.yaml}
---
output:
flexdashboard::flex_dashboard:
css: my-custom-rules.css
---
```
Then use your CSS rules in the body of your dashboard, for example:
``` {.html}
This is a <span class="my-purple">purple</span> word.
```
You may achieve the same result writing with Pandoc's spans:
``` {.html}
This is a [purple]{.my-purple} word.
```
You can also add CSS divs using Pandoc's [fenced Div blocks](https://pandoc.org/MANUAL.html#divs-and-spans):
``` {.html}
::: {.my-purple}
All of these words are purple.
:::
```
When knit, these blocks are translated into HTML like so:
``` {.html}
<div class="my-purple">
<p>All of these words are purple.</p>
</div>
```
Here is the knitted output that includes these custom rules:
{.snippet}
You can read more about Pandoc's custom blocks in the [R Markdown Cookbook](https://bookdown.org/yihui/rmarkdown-cookbook/custom-blocks.html).
## Add Sass rules
[Sass](https://sass-lang.com/) is a language designed for writing better CSS. Thankfully, the `css` parameters also understands `.scss` (i.e., Sass) file(s):
``` {.yaml}
---
output:
flexdashboard::flex_dashboard:
css: my-sass-rules.scss
theme:
version: 4
---
```
Sass files can contain [plain CSS rules](#using-css), as well as rules that use [Bootstrap Sass variables](https://rstudio.github.io/bslib/articles/bs4-variables.html). These variables are denoted by `$` followed by a variable name, like `$cyan`:
``` {.scss}
.bs4-bright {
background-color: $cyan;
color: $orange;
}
```
Then in your text you may use raw HTML or Pandoc syntax (as shown above):
``` {.html}
This is a [*bright*]{.bs4-bright} word.
```
``` {.html}
::: {.bs4-bright}
All of these words are *bright*.
:::
```
Here is the knitted output that includes these custom rules:
{.snippet}
You can find a full list of variables in `vignette("bs4-variables", package = "bslib")`.
When compiled, those Sass rules have access to Bootstrap Sass variables, functions, and mixins. This allows you to [write custom CSS styles in a "themeable" way](https://rstudio.github.io/bslib/articles/custom-components.html) and/or simply leverage useful Sass functions like [`color-contrast()`](https://github.com/rstudio/bslib/blob/df10a87/inst/sass-utils/color-contrast.scss#L9).
For example, we can use the Sass [`@extend`](https://sass-lang.com/documentation/at-rules/extend) rule in our `.scss` file to bundle together multiple Bootstrap variables and tell Sass that one selector should inherit the styles of another:
``` {.scss}
.error {
background-color: $cyan;
color: $white;
&--serious {
@extend .error;
color: $warning;
font-size: $display4-size;
}
}
```
Then you can use those rules in the body of your dashboard:
``` {.html}
::: {.error}
Uh oh...
:::
::: {.error--serious}
Time for a break.
:::
```
Here is the knitted output that includes these custom rules:
{.snippet}
You may also reuse any [fonts](#custom-themes) specified in your YAML with Sass variables.
| YAML key | Bootstrap 4 variable |
|----------------|--------------------------|
| `heading_font` | `$headings-font-family` |
| `base_font` | `$font-family-base` |
| `code_font` | `$font-family-monospace` |
So we could set up the following rule:
``` {.scss}
.pkg-name {
background-color: $cyan;
color: $white;
font-family: $font-family-monospace;
padding: .5rem;
}
```
Then use it in the body:
```{.html}
I like the [flexdashboard]{.pkg-name} package.
```
Here is the knitted output:
{.snippet}
## Resources
This vignette provides a brief overview of `{bslib}`'s integration with `{flexdashboard}`, but there's a lot more to theming with `{bslib}` (i.e. Bootstrap Sass), `{sass}` (i.e., Sass), and `{thematic}`. To learn more, see:
- The `{bslib}` package docs: <https://rstudio.github.io/bslib/>
- The `{sass}` package docs (for writing Sass code in R): <https://rstudio.github.io/sass/>
- The `{thematic}` package docs (for theming plot output): <https://rstudio.github.io/thematic/>
- Sass language docs: <https://sass-lang.com/documentation>
- Bootstrap docs: <https://getbootstrap.com/docs/4.0/>