Writing your thesis with R Markdown (1) – Getting started

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?

Languages and tools: R, Markdown, Rmarkdown v2, Latex, YAML, BibTeX
Software:
R & RStudio, pdflatex (MikTeX on windows, MacTeX on mac), Mendeley
R Package: knitr, use this code:

install.packages("knitr")
library(knitr)

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 hereR 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:

repro_res_ex1_decorated

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.

Good luck! And if you have any questions, feel free to leave a comment.

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.

13 thoughts on “Writing your thesis with R Markdown (1) – Getting started

  1. Pingback: Writing your thesis with R Markdown (3) – figures, R code and tables | Rosanna's Research

  2. Pingback: Writing your thesis with R Markdown (4) – Putting the thesis together | Rosanna's Research

  3. Pingback: Writing your thesis with R Markdown (5) – The thesis layout | Rosanna's Research

  4. When I knit to word I found that the equation 1 is not printed, but it work when I knit to pdf. Could you let me know why this occurs? Many thanks in advance!

  5. Thanks for these tutorials. I was thinking of Latex for my PhD thesis. But I have been using R Markdown to take notes of my data anlyses I became more confident with it and I would give it a try for my thesis.

  6. Pingback: Writing your thesis with R Markdown – paulvanderlaken.com

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s