Three More Ways to Make your RMarkdown Better

Eugene Olkhov
Jan 28 · 4 min read

Several months ago, I published an article which covered 3 simple ways to improve your RMarkdown. In this sequel article, I want to cover 3 more ways to improve your RMarkdown which deal with slightly more advanced techniques.

1. Adjusting CSS

In the previous article, I covered using built-in themes. These are really easy to use, and require a single line of code in the YAML of the RMarkdown. However, what if you want to modify a specific part of the theme such as the colors or font for a specific section? Here’s how you can do that:

  1. First, visit Bootswatch and look for the theme you want. This will have the built-in themes that are available in RMarkdown.
css: your_css_file_name.css

Once you have added it, it will behave exactly the same as the default theme, but will incorporate the changes you made.

Original theme “United”
Modified background color

2. Adding a logo

Often, you want to customize your RMarkdown with some branding. Here at CompassRed, we like to include our company logo at the top of the document. This can be done using a variety of ways. Typically, we do this by adding a short jQuery snippet right after the YAML like so:

$(document).ready(function() {
$('#section-TOC').parent().prepend('<div id=\"nav_logo\"><img src="logo_name.svg"></div>').css({"padding-top": "25px"});

The above can be customized in a variety of ways. We prefer our logo to sit on top of the table of contents in the top left corner.

The #section-TOC ID is the container for the table of contents. If you prefer the logo somewhere else, find the ID of the container you’re looking to target and input it into the jQuery (might take some modification of the parent function).

This code also assumes that the svg file of the logo exists in the same folder as the output html file. Alternatively, you can host the image somewhere and reference it in the src. This would be useful in instances where you need to host the output html file somewhere.

3. Chunk options

Finally, utilizing chunk options is another great way to customize your RMarkdown. There are many of these options that you can utilize. I will cover a few ways I personally use chunk options, but definitely check out the following source for ones I don’t mention.

Code folding is something that was briefly mentioned in the previous article. It’s useful if you are sharing your RMarkdown with different types of audiences, some of which may be interested in seeing your code. Code folding makes that easy by providing input buttons to either show or hide code. However, if you’re sharing with an audience that you know has no interest in seeing the code, you can omit the code output from individual chunks using the echo = F option.

{r, echo = F}

Alternatively, you can set the argument to TRUE if you want to force the code to be seen. You can also ignore warnings and messages if you would like. This is useful in the chunk which imports libraries since there’s often a lot of output that you wouldn’t want to clutter the document.

{r, echo = F, message = F, warning = F}

The final chunk option that I often use myself is setting figure sizes. Frequently, I find that the default aspect ratio of my ggplot2 visualizations isn’t quite how I want them to appear. For example, sometimes I want the viz to be wider in the output RMarkdown. For this I use the following:

{r, fig.width = 10, fig.height = 5}

The above gives me a 2:1 aspect ratio which usually works well. However, I encourage you to try out different sizes to see what works best for you.

This is using an aspect ration of 5:7
This is using a wider aspect ratio of 2:1

CompassRed Data Blog

We live for data and analytics.