Overview

In this workshop we will briefly examine how we can use R Markdown to generate reports that contain a blend of narrative, code, and the output of that code.

In the following video I will give you a brief overview of how you can turn a script you have written in R into an R Markdown document that you can ‘knit’ and share with others.

  

  

There are many resources available to help you explore the full range of possibilities in R Markdown. A good starting point is the “R Markdown: The Definitive Guide” by Yihui Xie, J. J. Allaire, and Garrett Grolemund. Just click on the image below to be taken to the online version of the book.

  

R Markdown Definitive Guide

  

You may also be interested in the “R Markdown Cookbook” by Yihui Xie, Christophe Dervieux, and Emily Riederer. Again, just click on the image below to be taken to the online version of the book.

  

R Markdown Cookbook

  

Your Challenge

Take one of the scripts that you’ve already written - maybe one of the visualisation scripts that you’ve developed - and use it to create an R Markdown document. Add the code from your script to a new R Markdown document in meaningful small code chunks (maybe just a few lines each) - you might have a code chunk that loads your libraries, a chunk that reads in your data, another chunk for wrangling your data, and then separate chunks for each of the visualisations you have written. Before each small chunk of code, add some narrative explaining what the following code chunk does. Tweak the message and warning parameters at the start of each code chunk so that warnings and messages aren’t displayed in the final R Markdown generated .html file.

Remember that when you’re knitting an R Markdown document it is working in its own R session - and so can’t access anything in the main R session in which you’ve been writing the R Markdown document itself. This means that your R Markdown must load the libraries needed and read in the data within the document itself.

All of the workshops in this unit have been written in R Markdown - so if you want to see how I’ve done that, you can click on the Improve this Workshop link at the bottom of any of the workshops and then take a look at the .Rmd file that will be in the script folder on the associated GitHub repo.

Improve this Workshop

If you spot any issues/errors in this workshop, you can raise an issue or create a pull request for this repo.