R Markdown

R Markdown1 is a powerful R package that allows users to dynamically render their work (code, graphs, and text) into various file formats. Its wide range of outputs (including HTML, PDF, Word) makes it one of the most flexible and intuitive communication tools within the R community. Ultimately, R Markdown bridges the gap between data professionals and the audience they need to communicate to. In fact, I’m communicating to you right now through an R Markdown document rendered to HTML.

In this blog post, I introduce my package, hmdrmd, which extends rmarkdown by providing document templates for data-oriented projects.
 

Tidy Analysis

While using R Markdown default files, I found that I was consistently reformatting my documents into same general structure. Being an acolyte of tidy data2, I organized my data projects into the following sections:

  • Packages
  • Import
  • Wrangle
  • Visualize
  • Model
  • Communicate / Export
     

This framework is based on a now infamous diagram that describes the “R for Data Science”3 workflow.

Figure 1. Tidy Data Analysis Workflow

This diagram is particularly useful for organizing a data project into a logical workflow.

To avoid continuously reformatting every new R Markdown document to this structure, I created this template, called “Tidy Analysis”.
 

Example of Tidy Analysis PDF output

 

Case Study Analysis

The second R Markdown template in hmdrmd is for case study analyses. This template is based on William Ellet’s case study guide in The Case Study Handbook, Revised Edition: A Student’s Guide4. This template is intended to be used by students in educational and research environments involving case analyses. This framework, in my opinion, is highly effective because it instructs students to pay attention to the most important issues and information in a case.

The document contains 12 essential steps, separated into 6 sections:

  • Role and Scenario
  • Criteria
  • Analysis
  • Evaluation
  • Uncertainties
  • Action Plan
     

Example of Case Study Analysis PDF output

 

Installation

You can download the package using the following code:

if (!requireNamespace("devtools")) install.packages("devtools")

devtools::install_github("HaydenMacDonald/hmdrmd")

 

Usage

In order to render pdf documents from R Markdown files, you will need LaTeX. If you have never installed a LaTex distribution, you can do so using the TinyTeX package.

install.packages("tinytex")

tinytex::install_tinytex()

 
Once hmdrmd and tinytex are installed, you can access these templates in RStudio through the following path:

File > New File > R Markdown… > From Template.

New R Markdown > From Template
 
Default settings for pdf output are controlled through the YAML header.

---
title: "title"
author: "author"
date: "2021-02-06"
output: 
  hmdrmd::case_study:
    highlight: tango
mainfont: CMU Serif Roman
monofont: Fira Code Medium
---

 
Remove or change these options from the YAML header to customize your document.

The default main font (mainfont) and code font (monofont) are set to CMU Serif Roman and Fira Code Medium, respectively. To use CMU Serif Roman and Fira Code Medium, follow their installation instructions. Additionally, you can change the syntax highlighting in code chunks through the highlight option in the YAML header. By default, it is set to tango. Other options include default, pygments, kate, monochrome, espresso, zenburn, and haddock. See this blog post by Garrick Aden-Buie for a gallery of these different options.  
 

Help

Submit issues on GitHub. Please feel free to extend this package by submitting a pull request!
 

Functionality

Currently, these templates only render to PDF documents. If you are interested in extending the functionality of this package, fork the original repository, make your changes and submit them as a pull request.
 

References

1: Allaire, JJ., Xie, Y., McPherson, J., Luraschi J., Ushey, K., Atkins, A., Wickham, H., Cheng, J., Chang, W., & Iannone, R. (2017). rmarkdown: Dynamic documents for R. R package version 1.11. URL: https://CRAN.R-project.org/package=rmarkdown

2: Wickham, H. (2014). Tidy data. Journal of Statistical Software, 59, URL: https://www.jstatsoft.org/article/view/v059i10

3: Wickham, H., & Grolemund, G. (2017). R for data science. Sebastopol, California: O’Reilly Media. URL: https://r4ds.had.co.nz/index.html

4: Ellet, W. (2018). The case study handbook, revised edition: A student’s guide. Boston, Massachussets: Harvard Business Review Press.