Chapter 7 Making your first Reproducible Document

RMarkdown by Allison Horst

Figure 7.1: RMarkdown by Allison Horst

This chapter is what makes your entire workflow sync.

There are several possible output formats that your RMarkdown file can render. They each have their subleties which are outside the scope of this book. PDF and HTML allow for the greatest customizability however, in academia I have found Microsoft Word is king. Your advisor, colleagues they all use it. So I am not going to attempt to re-invent the wheel here which is why we are knitting to *.docx. I am going to give you the essentials that you need to get started, that being said, there is a whole universe for you left to explore beyond this course if you so desire. There is a very strong argument to export using LaTEX, but in my experience most research teams are unfamiliar with LaTEX. For this reason, I prefer to use docx. If you do use a LaTEX environment it is easier to customize than docx and also has stronger background with templates (see the rticles packages).

RMarkdown Outputs

Figure 7.2: RMarkdown Outputs

RMarkdown Outputs

Figure 7.3: RMarkdown Outputs

See here

7.1 What goes into an Rmd document??

To answer this question effectively, we first need to consider what goes into a typical manuscript. Below is an illustration. As you can see, we have four main components:

  • Tables
  • Figures
  • Citations
  • Statistical results

We are going to go through each of these step by step in the next few headers.

7.2 Example Reproducible Document

This section assumes you have downloaded MyProjectName folder on your computer.

7.3 YAML Explained

YAML is the first part of code in your *.Rmd document. It controls how your document will be compiled. BE CAREFUL YAML is space and case sensitive.

Within YAML we find a few key components which we will go over now.

Insert your name Insert your project name Insert the path to your .bib file Change the .csl file to another format

7.3.1 Citations

Here we assume you have Zotero installed and you have exported your library. In the mean time you can use the one provided to you from my current project directory. Below in Figure 7.4 is an illustration of how citations work. There are two important files

  1. .bib file which contains your references
  2. .csl file which handles the formatting in-text

Before we get too far….mess around with YAML.

Insert your name Insert your project name Insert the path to your .bib file Change the .csl file to another format

knitr::include_graphics("images/bookdown_elements/RMarkdown-Citations.png")
Citations in RMarkdown

Figure 7.4: Citations in RMarkdown

Import a new citation into Zotero Add that citation into your Rmd document

7.4 Referencing a Figure

Figures can be inserted using a similar workflow. In Figure 7.5 is an illustration of how figures work

knitr::include_graphics("images/bookdown_elements/RMarkdown-Figures.png")
Figures in RMarkdown

Figure 7.5: Figures in RMarkdown

Notice how in the body of your Rmd document there is a code reference which gets called within your knitr chunk. There are a few rules when naming this code reference

  • ✅ dashes
    • \@ref(fig:my-awesome-table)
  • ⛔ spaces
    • \@ref(fig:myawesometable)
  • ⛔ underscores
    • \@ref(fig:my_awesome_table)
  • ⛔ periods
    • \@ref(fig:my.awesome.table)

A plot is shown in Figure \@ref(fig:myFigure1)

knitr::include_graphics("images/plot1.png")

7.4.1 Chunk Options/Elements

The first element that gets called is your code reference, followed by your desired caption in fig.cap.

Download an image off the internet Store it in /images folder Add a new reference and chunk in manuscript.Rmd Re-knit the document

7.5 Referencing a table

Tables work very similarly to figures

Descriptive statistics were run and are shown in Table \@ref(tab:journeyTime2) below

knitr::kable(iris, longtable=T, caption = "Descriptives")

Go into Excel and create a simple dataset Save it in /data folder Insert your own table into the Rmd
Re-knit the document

7.5.1 Results

For more in-depth description of other functions you can use in text I would go to the apastats folder on your computer R\win-library\4.0\apastats\html which will show a list of html files with descriptions.

There was a significant effect of service on the average journey time

7.5.2 In text statistics

This is common when describing demographics within an article. An example would be

“A convenience sample of 10 healthy controls (6 females, 4 males) aged XX ± YY years”

The value “XX” and “YY” can be replaced with code, such that if ever we update our dataset, this values get automatically updated.

knitr::include_graphics("https://i.imgur.com/4gdQ1kQ.png")
knitr::include_graphics("https://i.imgur.com/uIE5aV1.png")

7.5.3 Equation

Equation in RMarkdown use LaTeX encoding. I would recommend using a LaTeX equation maker online and pasting the code into your document. Here is an example.

7.6 Creating your own Word Template

Perhaps you don’t like the look of your current document. That’s ok!! Much of the formatting of your document is handled by a reference document, a template which knitr emulates when it renders your document. We can modify current styles and even create some * Create an MS Word Template for RMarkdown

7.7 Writing

Notice how headers are denoted using the # symbol.

7.9 Articles on Reproducibility