Three More Ways to Make your RMarkdown Better
By: Eugene Olkhov
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:
First, visit Bootswatch and look for the theme you want. This will have the built-in themes that are available in RMarkdown.
Download the bootstrap.css file associated with your theme — use the arrow dropdown to choose the right file.
Make modifications to the .css file using your preferred method.
In the YAML, include the following:
Once you have added it, it will behave exactly the same as the default theme, but will incorporate the changes you made.
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:
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.
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.
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:
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.
CompassRed is a full-service data agency that specializes in providing data strategy for clients across multiple industries.