Thea Knowles, R-Ladies #LdnOnt
January 14, 2020
bookdown
?knitr
When you run R Markdown, you are knitting together plain Markdown text and R code.
knitr
is the engine.
We will generate the following files:
We will also use the following files
bookdown
R package.bookdown
will automatically import knitr
and rmarkdown
pacakges as well. Install it by entering the following code into your R console:tidyverse
R packageinstall.packages(c("bookdown", "tidyverse")) # includes rmarkdown, knitr
Note: Workflow for a thesis is a slightly different beast.
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: - Thea Knowles^1^, Thea Knowles' Alter Ego^2^ - ^1^Western University, ^2^University of Western Ontario date: "14 January, 2020" output: bookdown::word_document2: fig_caption: yes md_extensions: +footnotes reference_docx: custom_reference.docx toc: yes date: "Last updated: ` r format(Sys.time(), '%d %B, %Y')`"
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.The briefest ever intro to Markdown syntax
Code >> Insert chunk
myChunk
)echo=TRUE
)Some other chunk 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 | dplyr:: starwars |
helper script | helper.R |
R Markdown summary document | starwars_summary_report.Rmd |
We will use the starwars
dataset included in the dplyr
package.
If you want a refresher of how to read in your own data, check out : - Check out Pierina’s R-Ladies materials for a refresher
library(dplyr) starwars %>% head()
## # A tibble: 6 x 13 ## name height mass hair_color skin_color eye_color birth_year gender ## <chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr> ## 1 Luke… 172 77 blond fair blue 19 male ## 2 C-3PO 167 75 <NA> gold yellow 112 <NA> ## 3 R2-D2 96 32 <NA> white, bl… red 33 <NA> ## 4 Dart… 202 136 none white yellow 41.9 male ## 5 Leia… 150 49 brown light brown 19 female ## 6 Owen… 178 120 brown, gr… light blue 52 male ## # … with 5 more variables: homeworld <chr>, species <chr>, films <list>, ## # vehicles <list>, starships <list>
Ingredient | In this example |
---|---|
data | dplyr:: starwars |
helper script | helper.R |
R Markdown summary document | starwars_summary_report.Rmd |
helper.R
source()
helper.RData: If your helper script takes a long time to run, you may instead find it useful to save its output to an .RData file:
save("helper.RData")
You can then load this .Rdata into your later reports instead of the raw .R script. You source an .R file:
source("helper.R")
You load an .Rdata file:
load("helper.RData")
Modular .R scripts: For bigger projects, I often break my R scripts down into smaller scripts, e.g.,
1_tidy.R, 2_models.R, 3_plots.R
In my workflows, each of these sources the last. I.e., the first line of 2_models.R
is
source("1_tidy.R")
And in my R Markdown file, I source the highest level or a final .RData file.
Ingredient | In this example |
---|---|
data | dplyr:: starwars |
helper script | helper.R |
R Markdown summary document | starwars_summary_report.Rmd |
starwars_summary_report.Rmd
.Rmd
Packages: Do you have all the packages installed?
install.packages(c("rmarkdown", "bookdown", "tidyverse"))
Directory structure: The following files should all be in the same directory:
apa.csl
custom_reference.docx
helper.R
rmd-bkdn.Rproj
starwars_refs.bib
Review as group
helper.R
sw_summary_human
data frame1. A chunk that sources helper.R
:
```{r source-helper}
source("../helper.R")
```
2. Another chunk that calls the sw_summary_human
data frame
```{r summary-df}
sw_summary_human
```
3. A line of text containing inline R code, e.g:
This average weight of all species is `\r mean(starwars$mass, na.rm=TRUE)`
helper.R
into 2 smaller scripts: 1_tidy.R
and 2_models.R
.source()
line and replace it with load()
, calling your new .RData file in.Is everything the same?
Ingredient | In this example |
---|---|
data | starwars |
helper script | helper.R |
R Markdown summary document | summary_report.Rmd |
Ingredient | In this example |
---|---|
data | starwars |
helper(s) | helper.R, starwars.RData, etc… |
R Markdown document(s) | manuscript.Rmd |
style reference .docx file (specify styles in your Word doc) | custom_reference.docx |
references in .bib format | starwars_refs.bib |
style bibliography (csl) file (how your bibliography will be formatted) | apa.csl |
We will start with the main body file
We can make use of many more options!
Notice I’ve used outputs from the bookdown
package here. Bookdown gives us way more functionality (an important one: cross-referencing figures and tables).
output: bookdown::word_document2: fig_caption: yes md_extensions: +footnotes reference_docx: ../custom_reference.docx toc: yes
bibliography: ../starwars_refs.bib
References must be in BibLaTex (.bib) format
Most reference managers can easily convert to .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].”
citr
Thea’s preferences here, full disclosure:
bibliography: /Users/thea/references.bib
Thea’s preferences here, full disclosure:
CSL: Citation Style Language
csl: ../apa.csl
apa.csl
biomed-central.csl
chicago-annotated-bibliography.csl
Several ways to do this! Here’s one:
images
; store your non-data-related images here```{r sw-logo, fig.cap = "Starwars logo", echo=FALSE}
knitr::include_graphics("../images/starwars.png")
```
```{r fig-sw, fig.cap = "Mass by Height for all species",
echo = FALSE, fig.align='center'}
sw_plot
```
fig-sw
fig.cap
Use knitr::kable()
```{r tab-sw-human, results='asis', echo = FALSE}
knitr::kable((sw_summary_human),
booktabs=T,
caption="Average height and mass for humans vs. non-humans")
```
knitr::kable()
will allow you to cross-reference easily later, but is not the only way.kable()
, not as a chunk optionflextable()
with the captioner
package as well.Caution:
Chunk options fig.align, out.width, out.height, out.extra are not supported for docx output
knitr::kable()
option, but out markdown table above did not get automatically numbered. If you want to include both R and non-R tables, I’d suggest either 1) converting markdown tables to R code or 2) using the flextable
package to generate captions and cross-references.bookdown
In the YAML, rather than specify outputs like word_document
or html_document
, we will now use the bookdown
versions of these: - e.g., bookdown::html_document2
, bookdown::word_document2
- This will allow us to automatically refer to Figures/Tables without having to explicitly refer to them by number - This is helpful if you wind up moving things around in your manuscript.
Syntax for cross-referencing with bookdown::word_document2
output requires you:
See Figure @ref(fig:fig-sw) below. See Table @ref(tab:tab-sw-human).
Page Piccinini’s R for Publication Lessons
Rosanna Van Hespen’s guide to writing your thesis with R Markdown
bookdown
to compile multiple R Markdown documents