March 6, 2018
When you run R Markdown, you are knitting together plain Markdown text and R code.
Required:
Other software to be aware of:
Copy and paste this code into your R console and run it
install.packages(c("rmarkdown","knitr","bookdown"))
install.packages(c("rmarkdown","knitr","bookdown"))
YAML (rhymes with camel): The header that tells R Markdown how to generate your document. Indentation and spacing are very important.
Basic:
title: "Untitled" author: "Thea Knowles" date: '2018-02-18' output: word_document
Gettin' fancy
title: "A very important title" subtitle: "A less important subtitle" author: name: Thea Knowles affiliation: Western University name: Scott Adams affiliation: Western University affiliation: University Hospital date: "09 March, 2018"
Even fancier:
Different options for:
tag=value
in the chunk header.
include
is set to FALSE
, indicating that we don't want the contents of this chunk included in the outputThis is the default chunk options set when you create a new .RMD (R Markdown) file.
```{r setup, include=FALSE} knitr::opts_chunk$set(echo = TRUE) ```
```{r setup, include=FALSE} knitr::opts_chunk$set(echo = TRUE) ```
This chunk provides the following information for "knitting" the document:
setup
: the name of the chunk You shouldn't have two chunks with the same name, unless they are unnamed (in which case they just get numbered automatically during the knit process)include = false
: the chunk will not be included in the output after knitting.knitr::opts_chunk$set(echo = TRUE)
: the default behavior for chunks is to "echo;" you will see the actual code printed in the final output. You can set this to false if you don't want the actual code included by default.Code >> Insert chunk
myChunk
)echo=TRUE
)Some other options
echo = FALSE
: don't show the code itself in the final documentinclude = FALSE
: don't include this code or its output in the final documenteval = FALSE
: don't actually run this code at allIngredient | In this example |
---|---|
data | starbucks_drinkMenu_expanded.csv |
helper script | helper.R |
R Markdown summary document | preliminary_results_summary.Rmd |
starbucks_drinkMenu_expanded.csv
helper.R
source()
preliminary_results_summary.Rmd
.Rmd
For now: Just open helper.R - Let's review it briefly 📚
command + R
OR click Run (top right of script pane)command + R
Got an error?
i.e., gotta report it to my supervisor
Make a new R Markdown document and store it in the same directory as your helper.R script
Just open preliminary_results_summary.Rmd
preliminary_results_summary.Rmd
Sometimes you may have to write multiple reports
Ingredient | In this example |
---|---|
data | starbucks_drinkMenu_expanded.csv |
helper script | helper.R |
R Markdown summary document | preliminary_results_summary.Rmd |
Ingredient | In this example |
---|---|
data | starbucks_drinkMenu_expanded.csv |
helper script | helper.R |
R Markdown document(s) | manuscript.Rmd, results.Rmd |
style reference .docx file (specify styles in your Word doc) | custom_reference.docx |
references in .bib format | starbucks_refs.bib |
style bibliography (csl) file (how your bibliography will be formatted) | apa.csl |
manuscript.Rmd
results.Rmd
Benefits:
We will start with the main body file
We can make use of many more options!
Let's focus on the word_document output for now
md_extensions: +footnotes
References in BibLaTex (.bib) format: starbucks_refs.bib
Use keys to cite a work in the markdown text, which will look like this in your markdown text:
"It is well known that some things are facts, and others are not [@knowles2018]."
Thea's preferences here, full disclosure:
bibliography: /Users/thea/references.bib
Thea's preferences here, full disclosure:
apa.csl
biomed-central.csl
chicago-annotated-bibliography.csl
Several ways to do this! Here are a few:
Have a subdirectory called images
; store your non-data-related images here

Just like how we did it in our summary report!
However: there are many more options within kable()
calsugsPlot
fig.cap
Unforunately, the standard word_document output doesn't allow for us to cross-reference Figures and Tables. You can still refer to Tables and Figures in the text, but you have to do it manually (i.e., explicitly type Figure 1, Table 2, etc.)
But… there's good news
bookdown::word_document2
Syntax for cross-referencing with Bookdown::word_document2 output:
Write a sentence in the results.Rmd that cites the article in the .bib file entitled "Fat and sugar: an economic analysis"
Create a new figure with a label and a caption, then reference it in a new sentence.
Make a new manuscript from scratch and set up the Intro, Methods, Results, Discussion, and References.
Rosanna Van Hespen's guide to writing your thesis with R Markdown