R Enterprise Meetup - 05/10/2021
Guidelines are usually colors, fonts, size but can also be document template.
We'll try cover most formats from HTML to Office Document
We often see these rmarkdown little wizard mixing Text & Code to produce document. Aim is to look deeper into this today. This is not so magic. It is juts perceived magic and there are tools to know about under the hood to be able to customize behavior further. And it is important to know about this for styling.
source: R Markdown Cookbook
knitr::knit()
+ Pandoc
(+ LaTeX
) = rmarkdown::render()
rmarkdown will run Pandoc & knitr for you so one input for multiple output possible. But stylings is specific to the output.
This is important to know all this because it helps know where to look and what could be tweak. one has to understand what to tweak to make something works as expected.
Let's talk about styling content now.
Cascading Stylesheets
While HTML is used to define the structure and semantics of your content, CSS is used to style it and lay it out.
For example, you can use CSS to alter the font, color, size, and spacing of your content, split it into multiple columns, or add animations and other decorative features.
Source: https://developer.mozilla.org/en-US/docs/Learn/CSS
Styling HTML output will require more or less CSS skills
If you have those skills, here what you can do
About the css
chunk engine
```{css, echo = FALSE}/* add a blue border */div.mybox { border-color: blue; border-style: solid; padding: 0.5em; text-align: center;}/* Set to blue bold text inside the box */div.mybox strong { color: blue;}```
Applied directly in the Rmd document without an external css file
Useful for prototyping, for quick iteration, for single file example echo = false is important if you don't want to show CSS source chunk in output
You can also use the css
argument of html_document()
But with CSS files it can be hard to create theme file you can reuse.
Enter SASS
About the sass
/scss
engine
Sass (https://sass-lang.com) is a CSS extension language that allows you to create CSS rules in much more flexible ways than you would do with plain CSS.
It allows for variables, tweaking functions (called mixins), operations (like /
), better CSS rule organisation (nesting, extensions, ...) and more.
NEW WAY
External tool used by web developers. Very powerful and this is a skill to learn when doing a lot of HTML. It can do a lot !
But no need to use it directly. Still need to learn the feature and syntax.
About the sass
/scss
engine
```{css, echo = FALSE}div.mybox { border-color: blue; border-style: solid; padding: 0.5em; text-align: center;}div.mybox strong { color: blue;}```
```{scss, echo = FALSE}$color1: blue;div { &.mybox { border-color: $color1; border-style: solid; padding: 0.5em; text-align: center; strong { color: $color1; } }}```
The CSS is the one that would be render when using sass on the .scss or .sass file.
No preference on the syntax - it is a personnal choice (and depending on environment.)
Quite new. Allow to use SASS from R. Wrapper around a C library.
Supported in rmarkdown now.
Powered by the new sass R 📦
External .scss
file can also be passed in the css
argument of most output format like html_document()
output: html_document: css: custom-style.scss
The file will be processed internally by sass::sass_file()
and produce a .css
automatically.
Using a .scss
file makes it easier to use variables and rules in CSS.
Use variables to store style guide values and use theme in several places.
You can do much more with SASS. It pushes the limit of customization of a document.
You can easily apply style on component identified by class or id, attributes.
HTML is the way to structure but Pandoc Markdown will simplify that.
Powered by Pandoc Markdown syntax
Creating custom blocks (using Pandoc's Fenced Divs)
::: myboxSpecial **important** content:::
With the previous style applied, it will result in this box in the document
Special important content
<div class="mybox"><p>Special <strong>important</strong> content</p></div>
Pandoc feature. R Markdown knows and extend it. Quite simple to write but quite powerful.
Supported by Visual editor.
Powered by Pandoc Markdown syntax
Using attributes on element works too (with Pandoc's Bracketed Spans)
[This is *some text*]{.my-bg-blue lang="en"}
Applying this style:
.my-bg-blue { background: lightblue;}
will result in:
This is some text
<p><span class="my-bg-blue" lang="en">This is <em>some text</em></span></p>
Powered by the new bslib R 📦
bslib provides tools for customizing Bootstrap themes directly from R. It allows take custom theming easier and provide easy access to pre-packaged Bootswatch themes.
Supported in R Markdown output format:
rmarkdown::html_document()
(using theme
argument)flexdashboard::flex_dashboard()
(current dev version > 0.5.2)bookdown::bs4_book()
built on Bootstrap 4Supported in other tools:
rmarkdown still doesn't use bslib by default: Simple example to activate Bootstrap 4 in html_document()
with bslib and default style
# Using bootswatch rmarkdown theme, # with included Bootstrap 3 (not bslib)output: html_document: theme: cerulean
# Activate the use of bslib, # with Bootstrap version 4 and not bootswatch themeoutput: html_document: theme: version: 4
# Activate the use of bslib, # with Bootstrap version 4 and bootswatch themeoutput: html_document: theme: bootswatch: cerulean
Showing a full example
Using variables in YAML instead of a .scss
file...
output: html_document: theme: primary: "#4D8DC9" # Changing primary color to blue secondary: "#4D4D4D" # Changing secondary to grey blockquote-border-color: "#FDBE4B" # Styling specific blockquote part to yellow lightblue: "#75AADB" # definining another blue as variable to reuse code-color: "$lightblue" # inline code color will be blue border-color: "$lightblue" # as default border color border-width: "3px" # increasing border width warning: "#E7553C" # warning-like text will be orange info: "#A4C689" # info like text will be green headings-font-weight: 900 # increasing heading font weight base_font: google: "Prompt" # changing base font code_font: google: "JetBrains Mono" # changing code font
Bootstrap is not for all outputs (xaringan, reveal, tufte, does not use for example)
We aim at making that even easier by creating a common theming system for all outputs where you could easily define vairables and rules and apply theme across output.
Going further with HTML styling in R
Customizing flexdashboard (development version): https://pkgs.rstudio.com/flexdashboard/articles/theme.html
Styling new bookdown::bs4_book()
: https://bookdown.org/yihui/bookdown/html.html#bs4-book
Using bslib: https://pkgs.rstudio.com/bslib
Using sass: https://pkgs.rstudio.com/sass
Styling pkgdown (development version): https://pkgdown.r-lib.org/dev/articles/customization.html
Using template reference document
Using a reference document as template with R Markdown
output: word_document: reference_doc: template.docx
output: powerpoint_presentation: reference_doc: template.pptx
One way to create a template:
Render once without reference document
Save the resulting output as the reference doc
Modify the reference document to customize according to your style
Use the document as reference doc
Example with Word Document
You can customize default style set in Headers, paragraph or else.
How to apply custom style ?
Using Div & Spans from Pandoc Markdown syntax.
Using fenced div attributes
for Paragraph style
::: {custom-style="highlight-box"}The **`r heaviest_specie`** specie is heavier than other ! :::
Using Spans attributes
for Text style:
[One of the species lives on all islands]{custom-style="Emphatically"} and the others are specific to one island only.
This will associate Docx Styles name to the text. Style can then be modified in template.
Documentation:
Examples:
Limitations & Beyond
It is powered by Pandoc
Unfortunatly, styling Office Document is not easy (through Pandoc or not)
Other great tools for R users
Officeverse by David Gohel
https://ardata-fr.github.io/officeverse
Producing PDF
Styling needs to be done using TeX styling mechanism, with help of CTAN packages.
🎉 CSS can be used !
Two solutions:
Printing document as-is
Formating and Styling HTML specifically for printing (using Paged Media CSS)
About Paged Media CSS with R
Paginate the HTML Output of R Markdown with CSS for Print https://github.com/rstudio/pagedown
pagedown brings Paged.js to R, a free and open source JavaScript library that paginates content in the browser to create PDF output from any HTML content.
📽 RSTUDIO::CONF 2019 Talk by Yihui Xie 📽
pagedown: Creating beautiful PDFs with R Markdown and CSS (RSTUDIO::CONF 2019)
and more examples in Github README
pagedown is the tool to make paginated HTML from R but designing paged report can be tricky.
Let's sum up!
Customize HTML output using CSS, SASS or Bootstrap variables with bslib
Using templates for Office outputs using Pandoc
Create and style custom blocks for HTML and office outputs using Pandoc Markdown
Create PDF from HTML to benefit from CSS styling
Paginated HTML is a thing, and can be done from R using pagedown
Keyboard shortcuts
↑, ←, Pg Up, k | Go to previous slide |
↓, →, Pg Dn, Space, j | Go to next slide |
Home | Go to first slide |
End | Go to last slide |
Number + Return | Go to specific slide |
b / m / f | Toggle blackout / mirrored / fullscreen mode |
c | Clone slideshow |
p | Toggle presenter mode |
t | Restart the presentation timer |
?, h | Toggle this help |
o | Tile View: Overview of Slides |
Esc | Back to slideshow |
R Enterprise Meetup - 05/10/2021
Guidelines are usually colors, fonts, size but can also be document template.
We'll try cover most formats from HTML to Office Document
We often see these rmarkdown little wizard mixing Text & Code to produce document. Aim is to look deeper into this today. This is not so magic. It is juts perceived magic and there are tools to know about under the hood to be able to customize behavior further. And it is important to know about this for styling.
source: R Markdown Cookbook
knitr::knit()
+ Pandoc
(+ LaTeX
) = rmarkdown::render()
rmarkdown will run Pandoc & knitr for you so one input for multiple output possible. But stylings is specific to the output.
This is important to know all this because it helps know where to look and what could be tweak. one has to understand what to tweak to make something works as expected.
Let's talk about styling content now.
Cascading Stylesheets
While HTML is used to define the structure and semantics of your content, CSS is used to style it and lay it out.
For example, you can use CSS to alter the font, color, size, and spacing of your content, split it into multiple columns, or add animations and other decorative features.
Source: https://developer.mozilla.org/en-US/docs/Learn/CSS
Styling HTML output will require more or less CSS skills
If you have those skills, here what you can do
About the css
chunk engine
```{css, echo = FALSE}/* add a blue border */div.mybox { border-color: blue; border-style: solid; padding: 0.5em; text-align: center;}/* Set to blue bold text inside the box */div.mybox strong { color: blue;}```
Applied directly in the Rmd document without an external css file
Useful for prototyping, for quick iteration, for single file example echo = false is important if you don't want to show CSS source chunk in output
You can also use the css
argument of html_document()
But with CSS files it can be hard to create theme file you can reuse.
Enter SASS
About the sass
/scss
engine
Sass (https://sass-lang.com) is a CSS extension language that allows you to create CSS rules in much more flexible ways than you would do with plain CSS.
It allows for variables, tweaking functions (called mixins), operations (like /
), better CSS rule organisation (nesting, extensions, ...) and more.
NEW WAY
External tool used by web developers. Very powerful and this is a skill to learn when doing a lot of HTML. It can do a lot !
But no need to use it directly. Still need to learn the feature and syntax.
About the sass
/scss
engine
```{css, echo = FALSE}div.mybox { border-color: blue; border-style: solid; padding: 0.5em; text-align: center;}div.mybox strong { color: blue;}```
```{scss, echo = FALSE}$color1: blue;div { &.mybox { border-color: $color1; border-style: solid; padding: 0.5em; text-align: center; strong { color: $color1; } }}```
The CSS is the one that would be render when using sass on the .scss or .sass file.
No preference on the syntax - it is a personnal choice (and depending on environment.)
Powered by the new sass R 📦
External .scss
file can also be passed in the css
argument of most output format like html_document()
output: html_document: css: custom-style.scss
The file will be processed internally by sass::sass_file()
and produce a .css
automatically.
Using a .scss
file makes it easier to use variables and rules in CSS.
Use variables to store style guide values and use theme in several places.
You can do much more with SASS. It pushes the limit of customization of a document.
You can easily apply style on component identified by class or id, attributes.
HTML is the way to structure but Pandoc Markdown will simplify that.
Powered by Pandoc Markdown syntax
Creating custom blocks (using Pandoc's Fenced Divs)
::: myboxSpecial **important** content:::
With the previous style applied, it will result in this box in the document
Special important content
<div class="mybox"><p>Special <strong>important</strong> content</p></div>
Pandoc feature. R Markdown knows and extend it. Quite simple to write but quite powerful.
Supported by Visual editor.
Powered by Pandoc Markdown syntax
Using attributes on element works too (with Pandoc's Bracketed Spans)
[This is *some text*]{.my-bg-blue lang="en"}
Applying this style:
.my-bg-blue { background: lightblue;}
will result in:
This is some text
<p><span class="my-bg-blue" lang="en">This is <em>some text</em></span></p>
Powered by the new bslib R 📦
bslib provides tools for customizing Bootstrap themes directly from R. It allows take custom theming easier and provide easy access to pre-packaged Bootswatch themes.
Supported in R Markdown output format:
rmarkdown::html_document()
(using theme
argument)flexdashboard::flex_dashboard()
(current dev version > 0.5.2)bookdown::bs4_book()
built on Bootstrap 4Supported in other tools:
rmarkdown still doesn't use bslib by default: Simple example to activate Bootstrap 4 in html_document()
with bslib and default style
# Using bootswatch rmarkdown theme, # with included Bootstrap 3 (not bslib)output: html_document: theme: cerulean
# Activate the use of bslib, # with Bootstrap version 4 and not bootswatch themeoutput: html_document: theme: version: 4
# Activate the use of bslib, # with Bootstrap version 4 and bootswatch themeoutput: html_document: theme: bootswatch: cerulean
Showing a full example
Using variables in YAML instead of a .scss
file...
output: html_document: theme: primary: "#4D8DC9" # Changing primary color to blue secondary: "#4D4D4D" # Changing secondary to grey blockquote-border-color: "#FDBE4B" # Styling specific blockquote part to yellow lightblue: "#75AADB" # definining another blue as variable to reuse code-color: "$lightblue" # inline code color will be blue border-color: "$lightblue" # as default border color border-width: "3px" # increasing border width warning: "#E7553C" # warning-like text will be orange info: "#A4C689" # info like text will be green headings-font-weight: 900 # increasing heading font weight base_font: google: "Prompt" # changing base font code_font: google: "JetBrains Mono" # changing code font
Bootstrap is not for all outputs (xaringan, reveal, tufte, does not use for example)
We aim at making that even easier by creating a common theming system for all outputs where you could easily define vairables and rules and apply theme across output.
Going further with HTML styling in R
Customizing flexdashboard (development version): https://pkgs.rstudio.com/flexdashboard/articles/theme.html
Styling new bookdown::bs4_book()
: https://bookdown.org/yihui/bookdown/html.html#bs4-book
Using bslib: https://pkgs.rstudio.com/bslib
Using sass: https://pkgs.rstudio.com/sass
Styling pkgdown (development version): https://pkgdown.r-lib.org/dev/articles/customization.html
Using template reference document
Using a reference document as template with R Markdown
output: word_document: reference_doc: template.docx
output: powerpoint_presentation: reference_doc: template.pptx
One way to create a template:
Render once without reference document
Save the resulting output as the reference doc
Modify the reference document to customize according to your style
Use the document as reference doc
How to apply custom style ?
Using Div & Spans from Pandoc Markdown syntax.
Using fenced div attributes
for Paragraph style
::: {custom-style="highlight-box"}The **`r heaviest_specie`** specie is heavier than other ! :::
Using Spans attributes
for Text style:
[One of the species lives on all islands]{custom-style="Emphatically"} and the others are specific to one island only.
This will associate Docx Styles name to the text. Style can then be modified in template.
Documentation:
Examples:
Limitations & Beyond
It is powered by Pandoc
Unfortunatly, styling Office Document is not easy (through Pandoc or not)
Other great tools for R users
Officeverse by David Gohel
https://ardata-fr.github.io/officeverse
Producing PDF
Styling needs to be done using TeX styling mechanism, with help of CTAN packages.
🎉 CSS can be used !
Two solutions:
Printing document as-is
Formating and Styling HTML specifically for printing (using Paged Media CSS)
About Paged Media CSS with R
Paginate the HTML Output of R Markdown with CSS for Print https://github.com/rstudio/pagedown
pagedown brings Paged.js to R, a free and open source JavaScript library that paginates content in the browser to create PDF output from any HTML content.
📽 RSTUDIO::CONF 2019 Talk by Yihui Xie 📽
pagedown: Creating beautiful PDFs with R Markdown and CSS (RSTUDIO::CONF 2019)
and more examples in Github README
pagedown is the tool to make paginated HTML from R but designing paged report can be tricky.
Let's sum up!
Customize HTML output using CSS, SASS or Bootstrap variables with bslib
Using templates for Office outputs using Pandoc
Create and style custom blocks for HTML and office outputs using Pandoc Markdown
Create PDF from HTML to benefit from CSS styling
Paginated HTML is a thing, and can be done from R using pagedown