class: center, middle, inverse, title-slide # Business Reports with R Markdown ## How to better theme report ? ### Christophe Dervieux ###
R Enterprise Meetup - 05/10/2021
---
# Who am I ? .subtitle[Short intro] .center[ .profile[![:scale 20%, profile picture for Christophe Dervieux](https://avatars.githubusercontent.com/u/6791940?v=4)] Christophe DERVIEUX • France RStudio • Software Engineer • R Markdown Team ] </br> .pull-left[ .center[ .f2.color1[
]</br> [@cderv](https://github.com/cderv) ] ] .pull-right[ .center[ .f2.color1[
]</br> [@chrisderv](https://twitter.com/chrisderv) ] ] ??? --- # What are we talking today ? .center-middle[ > I work in an organization with some styling guidelines and I would like to use them on my R Markdown document. > > How does that work ? .center.pv4.big[Let's discuss about styling with R Markdown !] ] ??? Guidelines are usually colors, fonts, size but can also be document template. We'll try cover most formats from HTML to Office Document --- layout: true # What happens when it renders ? --- .center[![:scale 75%, R Markdown wizard illustration by Alison Horst](rmarkdown_wizards.png)] .source-fig.center[Source: https://github.com/allisonhorst/stats-illustrations] ??? 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. --- .center[![:scale 75%, Diagram showing the different step of rendering, using knitr first from Rmd to md, then Pandoc from md to output format like HTML, DOC and PDF (using LaTeX)](https://bookdown.org/yihui/rmarkdown-cookbook/images/workflow.png)] .source-fig.center[ source: [R Markdown Cookbook](https://bookdown.org/yihui/rmarkdown-cookbook) ] .box.f3[`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. --- layout: false class: inverse .center[![:scale 65%, Gif of a man in a suits putting a a pink hat with included sunglasses. Text on the gif says 'You don't like my style', then 'Deal with it'](https://media.giphy.com/media/14o3gppq0mt2Lu/giphy.gif)] ??? Let's talk about styling content now. --- class: inverse middle # Working with HTML output --- # Styling with CSS .subtitle[Cascading Stylesheets] ## What is CSS ? .center-middle[ > 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-fig.center[Source: https://developer.mozilla.org/en-US/docs/Learn/CSS] .box[ .ma3[ Styling HTML output will require more or less CSS skills ] ] ] ??? If you have those skills, here what you can do --- # Use CSS from inside a Rmd file .subtitle[About the `css` chunk engine] ````markdown ```{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 --- layout: true # Use SASS the same way .subtitle[About the `sass`/`scss` engine] --- ## What is SASS ? 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. </br> </br> .center[![:scale 20%](https://sass-lang.com/assets/img/logos/logo-b6e1ef6e.svg)] 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. --- .panelset[ .panel[.panel-name[previous css] ````markdown ```{css, echo = FALSE} div.mybox { * border-color: blue; border-style: solid; padding: 0.5em; text-align: center; } div.mybox strong { * color: blue; } ``` ```` ] .panel[.panel-name[scss] ````markdown ```{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.) --- layout: true # Use SASS the same way .subtitle[Powered by the new **sass** R 📦] --- .center[ ![:scale 25%](https://rstudio.github.io/sass/reference/figures/logo.svg) https://pkgs.rstudio.com/sass/ .box[Support is now built-in **rmarkdown**] ] ??? Quite new. Allow to use SASS from R. Wrapper around a C library. Supported in rmarkdown now. --- External `.scss` file can also be passed in the `css` argument of most output format like `html_document()` </br> ````yaml output: html_document: css: custom-style.scss ```` .pb3[ The file will be processed internally by `sass::sass_file()` and produce a `.css` automatically. ] .box[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. --- layout: true # Apply custom style on specific component .subtitle[Powered by Pandoc Markdown syntax] --- Creating custom blocks (using Pandoc's Fenced Divs) ```markdown ::: mybox Special **important** content ::: ``` <style type="text/css"> .mybox-demo { border-color: blue; border-style: solid; padding: 0.5em; text-align: center; } .mybox-demo strong { color: blue; } </style> .pb3[ With the previous style applied, it will result in this box in the document ] .pb3[ .mybox-demo[ Special **important** content ] ] ```html <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. --- Using attributes on element works too (with Pandoc's Bracketed Spans) ```markdown [This is *some text*]{.my-bg-blue lang="en"} ``` Applying this style: ```css .my-bg-blue { background: lightblue; } ``` <style type="text/css"> .my-bg-blue { background: lightblue; } </style> will result in: .my-bg-blue[This is *some text*] ```html <p><span class="my-bg-blue" lang="en">This is <em>some text</em></span></p> ``` --- layout: false # Going further with Bootstrap .subtitle[Powered by the new **bslib** R 📦] [**bslib**](https://pkgs.rstudio.com/bslib) provides tools for customizing [Bootstrap](https://getbootstrap.com/) themes directly from R. It allows take custom theming easier and provide easy access to pre-packaged Bootswatch themes. .pull-left[ Supported in R Markdown output format: * `rmarkdown::html_document()` .small[(using `theme` argument)] * `flexdashboard::flex_dashboard()` .small[(current dev version > 0.5.2)] * `bookdown::bs4_book()` built on Bootstrap 4 ] .pull-right[ Supported in other tools: * **DT** (since 0.18) * **shiny** (since 1.6.0) * **pkgdown** (current dev version > 1.6.1) ] **rmarkdown** still doesn't use **bslib** by default: Simple example to activate Bootstrap 4 in `html_document()` with **bslib** and default style .fl.w-third.ph1[ ```yaml # Using bootswatch rmarkdown theme, # with included Bootstrap 3 (not bslib) output: html_document: theme: cerulean ``` ] .fl.w-third.ph1[ ```yaml # Activate the use of bslib, # with Bootstrap version 4 and not bootswatch theme output: html_document: theme: version: 4 ``` ] .fl.w-third.ph1[ ```yaml # Activate the use of bslib, # with Bootstrap version 4 and bootswatch theme output: html_document: theme: bootswatch: cerulean ``` ] ??? --- layout: true # How does that work ? .subtitle[Showing a full example] --- Using variables in YAML instead of a `.scss` file... ```yaml 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 ``` --- ...to get a customized document following specific style .fl.w-90[ .center[![:scale 68%, Screenshot of resulting html document using previous yaml header](style-based-bootstrap.png)] ] .fl.w-10[ .small[ Demo file: * [Source Rmd](styling-bootstrap.Rmd) * [HTML output](styling-bootstrap.html) ] ] ??? 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. --- layout: false # Useful resources .subtitle[Going further with HTML styling in R] .pv5[ * 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 ] --- class: inverse middle # Working with Office document --- layout: true # Styling Word and Powerpoint --- .subtitle[Using template reference document] Using a reference document as template with R Markdown .fl.w-50.pr4[ ```yaml output: word_document: reference_doc: template.docx ``` ] .fl.w-50.pl4[ ```yaml output: powerpoint_presentation: reference_doc: template.pptx ``` ] .underline[One way to create a template:] 1. Render once without reference document 1. Save the resulting output as the reference doc 1. Modify the reference document to customize according to your style 1. Use the document as reference doc --- .subtitle[How to customize a default template ?] .center[ ![:scale 70%](change-style-in-word.png) ] ??? Example with Word Document You can customize default style set in Headers, paragraph or else. --- .subtitle[How to apply custom style ?] Using [Div & Spans](https://pandoc.org/MANUAL.html#divs-and-spans) from Pandoc Markdown syntax. .fl.w-50.pr5[ Using fenced div attributes for Paragraph style ````markdown ::: {custom-style="highlight-box"} The **`r heaviest_specie`** specie is heavier than other ! ::: ```` ] .fl.w-50.pl5[ Using Spans attributes for Text style: ```markdown [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. .fl.w-50[ Documentation: * [Pandoc Manual about Custom Styles](https://pandoc.org/MANUAL.html#custom-styles) * [Style name to tweak](https://pandoc.org/MANUAL.html#option--reference-doc) ] .fl.w-50[ Examples: * [Rmd source](pinguins-report.Rmd) * [Docx template](template.docx) * [Docx output](pinguins-report.docx) ] --- .subtitle[Limitations & Beyond] .f3.color1.b[It is powered by Pandoc] * We're limitated by their features - not everything can be customized. * But we worked this summer with Pandoc team to improve Powerpoint templating mechanism. .f3.color1.b[Unfortunatly, styling Office Document is not easy (through Pandoc or not)] * One must know about Office Style system * Simple customization works, more complex may not .f3.color1.b[Other great tools for R users] .fl.w-third.center[![:scale 50%](https://ardata-fr.github.io/officeverse/illustrations/officerlogo.svg)] .fl.w-third.center[![:scale 50%](https://ardata-fr.github.io/officeverse/illustrations/officedown.svg)] .fl.w-third.center.pt5[ Officeverse by David Gohel .small[https://ardata-fr.github.io/officeverse]] --- layout: false class: inverse middle # Working with PDF document --- layout: true # What about PDF output ? --- .subtitle[Producing PDF] ## Creating PDF through LaTeX Styling needs to be done using TeX styling mechanism, with help of CTAN packages. ## Creating PDF through HTML 🎉 CSS can be used ! Two solutions: 1. Printing document as-is 2. Formating and Styling HTML specifically for printing (using Paged Media CSS) --- .subtitle[About Paged Media CSS with R] .flex.items-center[ .w-25[ .center[![:scale 50%](https://raw.githubusercontent.com/rstudio/hex-stickers/master/SVG/pagedown.svg)] ] .w-75[ Paginate the HTML Output of R Markdown with CSS for Print .center[https://github.com/rstudio/pagedown] ] ] .pv3[ **pagedown** brings [Paged.js](https://www.pagedjs.org/) to R, a free and open source JavaScript library that paginates content in the browser to create PDF output from any HTML content. ] .box[ 📽 .color1[RSTUDIO::CONF 2019 Talk by Yihui Xie] 📽 [pagedown: Creating beautiful PDFs with R Markdown and CSS (RSTUDIO::CONF 2019) ](https://www.rstudio.com/resources/rstudioconf-2019/pagedown-creating-beautiful-pdfs-with-r-markdown-and-css/) and more examples in Github README ] ??? **pagedown** is the tool to make paginated HTML from R but designing paged report can be tricky. --- .subtitle[Simplify the use of **pagedown** through templates] .fl.w-30[ ![:scale 100%](https://rfortherestofus.com/wp-content/uploads/2021/01/windmill.gif) ] .fl.w-70.pa3[ Enter **pagedreport** 📦 by Thomas Vroylandt and David Keyes .center.small[[Announcing pagedreport](https://rfortherestofus.com/2021/01/announcing-pagedreport/) • [Docs](https://pagedreport.rfortherestofus.com/)] * Uses R Markdown to produce PDF using **pagedown** * Offers some template than can be tweak easily * Colors * Fonts * Covers * Logo ] --- layout: false # What we've learn so far ? .subtitle[Let's sum up!] .pv4[ * .color2[**Customize HTML output**] using CSS, SASS or Bootstrap variables with **bslib** * .color2[**Using templates**] for Office outputs using Pandoc * .color2[**Create and style custom blocks**] for HTML and office outputs using Pandoc Markdown * .color2[**Create PDF from HTML**] to benefit from CSS styling * .color2[**Paginated HTML is a thing**], and can be done from R using **pagedown** ] .center[![:scale 30%](https://media.giphy.com/media/S8Bnf6KByXDly5VCzq/giphy.gif)] --- class: center middle inverse # Thank you ! .f2.white[
]</br>https://github.com/cderv/meetup-2021-rmd-business-report