Looking to use R Markdown to create your thesis? You have come to the right place. R Markdown is a great tool for integrating data analysis and report writing, but it can be a bit daunting to get started. I wrote my thesis with R Markdown last year, and sure spent quite some time browsing the internet figuring out how to get my text, code, and figures to work together and turn into a coherent thesis. But it was worth the struggle (and an excellent way to procrastinate), and I’d love to share with you what I’ve learned.
This will be a series of blog posts discussing how to write a thesis with R Markdown. We’ll start with the very basics: installing the necessary software and getting familiar with the various markup languages you’ll need. Then we’ll prepare an R Markdown document that will represent one thesis chapter. When I wrote my thesis, I wanted to do the following things in each chapter:
- Writing text
- Spelling check this text
- Comment on this text
- Writing equations
- With references
- And captions
- Including figures (non-R creations)
- With references
- And captions
- Citing stuff
- And add a bibliography
- Adding R code
- Render R code
- Add figure captions to R rendered figures + references
- Including tables
- Really difficult tables
In the next blog post, I will explain how to do each of these things in a thesis chapter (UPDATE: I have decided to split the chapter contents blog post into to blog posts, 1: text, citations and equations and 2: figures, R code and tables). Then, I’ll show how to use multiple documents together, and get the fiddly layout bits like page numbering, title pages and table of contents stuff working to create a full thesis document. But first, let’s get started with the basics.
Getting started: What do we need?
Don’t worry, it sounds way scarier than it actually is. Markdown is a very simple and straightforward language. No need to worry about it now, but if you’d like to get more familiar with it have a look at this reference guide or learn it here. R Markdown is almost the same as Markdown, but has the added feature off embedding and dealing with R code in your documents. LaTeX is another markup language, that is not as easy to read and write as Markdown but allows for much more flexibility in creating your document. The code that is placed at the top of document is called YAML, and it explains how the document should be rendered. BiBTeX is an excellent reference management system (if you are used to the horror of inserting references with MS word add-ons, you are going to be really happy using this :). BiBTeX files can be created with Mendeley, more on this later.
Here is an example of an R Markdown document, and how each of the languages are used:
Click on the image to enlarge.
You can see there’s quite a few different languages used in the same document. In the next blog post we will go into the details of each of these parts of the document. For now, (once everything is installed and communicating with each other – if you are having problems, feel free to leave a question in the comments), I recommend to just play around with R Markdown for a bit to get a feel for it. You can do this by going to Rstudio > File > New File > R markdown… > OK. This opens a standard R Markdown file (.rmd) with some instructions.
You may find yourself worrying about this at some poin (probably not yet, but when you do, you’ll know where to look): there are multiple types of markdown, which are called flavours. Examples are ‘github markdown’, ‘multiple markdown’ and ‘pandoc markdown’. The markdown we are using here is called pandoc markdown.