Reproducible HR Analytics, Part 2

In the previous post I talked about the value of reproducible research and provided a bare-bones introduction to R Markdown, a great vehicle for combining data, code, analysis, and visualizations into a single, shareable package.

In today’s post, I’ll answer a few questions that will likely pop up when you are digging into reproducible research using Rmarkdown.

Some are more technical, others less so but all are intended to save you some time and headaches.

If you have other “How do I…” sorts of questions please reach out and I will do my best to answer them.

This is a living list and I expect it will grow over time.

Please also note that a number of these are also available in the R Markdown cheatsheet which you can find here

That cheatsheet might seem a bit overwhelming at first but once you get started you’ll find it invaluable.

How Do I …

1. Structure My Folders?

I strongly recommend that you use a consistent file naming structure for all of your work. Naming things the same way will make it easier for you to find files and know where to put them when you are coding. This sounds like a little thing but having all of things decided for you in advance eliminates all of those small decisions and searches that can really eat up your time.

First, start with a dedicated project folder with a clear, simple name.

In that project folder add the following:

  • An R Project file; this is what you should use to launch your R work and make sure you are always in the right working directory and have all of your files available.
  • lib folder: scripts for any special function that you create
  • data folder: folder to hold the data you need for your project;
  • rmd folder: where you will save your Rmd files
  • output folder: to hold any output files for your project
  • scratch folder: a place to save little script files that I sometimes need to test things or refer to later but which are not necessarily critical for the project per se.

2. Hide My Code?

If you don’t want any of your code visible at all in the HTML or PDF output you create with your R Markdown, set the knitr global options to echo = FALSE.

You can override this on a case-by-case basis by setting echo = TRUE in the individual code chunk headers.

If you want your code to be available in your document but you don’t want it to dominate the whole thing, you can use “code folding”.

Code foldering makes the code hidden initially but available if you click a button in the HTML doc.

My output element in the YAML header looks like this (including both pieces for code folding and also a table of contents):

      code_folding: hide
      number_sections: TRUE
      toc: true
      toc_float: true

3. Not Run Select Code Chunks?

If you want to keep code in the Rmd but you don’t want to run the code, set the code block option for evaluation to FALSE.

  `{r eval = FALSE}`

This will apply to the entire code block.

For smaller pieces you can just comment out the individual lines as usual useing #.

4. Add a Table of Contents?

Set your YAML header to include a TOC (table of contents). My YAML header for output looks like the following:

      code_folding: hide
      number_sections: TRUE
      toc: true
      toc_float: true

Experiment with the TRUE/ FALSE values to see what happens.

Additional information on creating a table of contents can be found here

6. Select Color for the TOC?

Assuming you have a table of contents already set up, you can use the following bit of html-style code to change the color in your R Markdown document.

  <style>,, {
      background-color: #f26e3f;

All you need to do is just change that last bit of color code to match your company’s branding colors.

This code should go directly into an Rmd text section, NOT in a code block.

Rmd will recognize it as html code and process it accordingly.

I would put this at the top just below your yaml header and your global options code block.

I owe this one to this post on stackoverflow.


Contact Us

Yes, I would like to receive newsletters from HR Analytics 101.