Styling RMarkdown docs
This lesson is called Styling RMarkdown docs, part of the R in 3 Months (Fall 2022) course. This lesson is called Styling RMarkdown docs, part of the R in 3 Months (Fall 2022) course.
We've had a few questions about how to change styles in RMarkdown, for instance how to change fonts or how to center all headings in a document.
The R in 3 Months syllabus switches to Advanced RMarkdown in the final week, and David will walk you through customising styles (and we'll set you and assignment). However! I'm also going to collate together some information now so you're prepared (and know what's possible). If you have specific questions please do ask them in the thread.
Output type matters
RMarkdown allows us to generate PDF, HTML, Word documents and even PowerPoint presentations. However, you'll need to use different approaches to customise each of these file types â because they're fundamentally different to one another.
HTML
HTML RMarkdown documents are great because they can be published to the web and even contain interactive charts, maps and other data visualisations.
We style HTML with CSS (Cascading Style Sheets) which are text files saved into your RStudio project and included into your RMarkdown YAML header.
---
title: "Habits"
output:
html_document:
css: styles.css
---
I findw3schools.com useful for learning about CSS.
However, CSS can quickly become quite complicated. Thankfully the awesome {prettydoc} package containsseveral built-in themes for HTML RMarkdown. The package also makes it fairly easy to modify the built-in themes, which is easier than building your own CSS from scratch.
It's also important to note that HTML itself must be used to control HTML RMarkdown documents. Here are some examples:
# This would add a line break
<br>
# This would insert an image
<img src='https://upload.wikimedia.org/wikipedia/commons/thumb/1/1b/R_logo.svg/2560px-R_logo.svg.png'/>
PDF RMarkdown documents are great because PDFs are one of the most widely vieweable file types in existence. They're also great for printing.
If you were to create a pdf_document with RMarkdown you would need to learn LaTeX to format your document. It's infamously powerful but it a more technical thing to learn than HTML.
Thankfully, the {pagedown} package allows you to create a paged HTML document which you can then easily export to a PDF by using your browsers print page functionality. This means you can use HTML and CSS to style RMarkdown documents that are destined for either the web or print.Â{pagedown} has extensive documentation in the form of a book. It's also fairly easy to build your own {pagedown} templates which [David has used to created {pagedreport}](several attractive PDF templates ).
MS Word
Word documents are great because sometimes you need to generate Word documents â you have an existing workflow that wouldn't support PDF or HTML.
Word documents are styled using reference documents. There are two excellent resources on how to use these:
RStudio's article,"Happy collaboration with Rmd to docx" Daniel Hadley's presentation on"Branding and automating your work with R Markdown"
Have any questions? Put them below and we will help you out!
Course Content
142 Lessons
You need to be signed-in to comment on this post. Login.