-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.Rmd
284 lines (183 loc) · 5.66 KB
/
index.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
---
title: An introduction to R Markdown
subtitle: |
| West Lab, Dept. Biological Sciences, UCT
| ![](images/BIO.png){ width=25px } ![](images/UCT.png){ width=25px }
author: Ruan van Mazijk
date: '`r Sys.Date()`'
fig_caption: yes
mainfont: Avenir
bibliography: example.bib
---
Please refer to the [pre-tut setup](pre-tut-setup.html), if you haven't already, before we begin.
# What is R Markdown?
R Markdown is a document preparation system, like MS Word, but completely different. Importantly, it works in plain-text and is highly accessible, open source, and makes it really easy to embed R-code in documents (e.g. to create figures or tables).
***
## Document preparation systems
Document prep systems vary. There are those where what you **see** is what you **get** in the final document ("WYSIWYG"-systems; e.g. MS Word), and there are those where what you **see** is what you **mean** ("WYSIWYM"-systems; e.g. markup languages such as HTML, LaTeX).
R Markdown is based on the markup language "Markdown". Mark**down** was invented to be a simpler alternative to more complicated mark**up** languages like HTML and LaTeX. These markup languafes are often quite hard to read in raw-form and even harder to write. See for yourself:
***
![HTML](images/HTML.png)
***
![LaTeX](images/LaTeX.png)
# The benefits of R Markdown
What R Markdown does is extend Markdown by making R-code (and other programming languages) executable from within the document's source file, allowing the results to show up in the final document (e.g. figures or tables), thereby "weav[ing] together narrative text and code to produce elegantly formatted output" (<https://rmarkdown.rstudio.com>).
It also adds a bit more functionality to Markdown with simple syntax for in-text references, equations, and more.
And, best of all, it makes the results of any analysis fully reproducible!
# How does R Markdown work?
R Markdown takes the file you write (e.g. `analysis.Rmd`), converts it to plain markdown using the R-package `knitr`, then converts it any of the output formats you choose, using the open source software `pandoc`.
![R Markdown flowchart (<https://rmarkdown.rstudio.com/lesson-2.html>)](images/rmarkdownflow.png)
# Possible output formats
![](images/possible-output-formats.png)
***
An R Markdown (`.Rmd`) file has two main components:
1. the YAML header
2. and the body
# Example
`analysis.Rmd` might look like this:
---
title: My analysis
author: Ruan van Mazijk
date: 2019-11-15
output: html_document
---
# Introduction
Blah blah blah blah ...
# Methods
Etc. etc. etc. ...
***
## Rendering your document
Use the output specified in the header
rmarkdown::render("analysis.Rmd")
Or over-ride it
rmarkdown::render("analysis.Rmd"
output_format = "pdf_document"
)
***
## Using Markdown-style markup
***
### Headings
# A heading
## A sub-heading
### A sub-sub-heading
(Can go down 6 levels)
***
### Unordered lists
- Item
- Item
- Item
- Sub-item
- Sub-item
- Sub-sub-item
- Etc.
***
### Unordered lists cont.
- Item
- Item
- Item
- Sub-item
- Sub-item
- Sub-sub-item
- Etc.
***
### Ordered lists
1. Item
2. Item
3. Item
a. Sub-item
b. Sub-item
1. Sub-sub-item
2. Etc.
***
### Ordered lists cont.
1. Item
2. Item
3. Item
a. Sub-item
b. Sub-item
1. Sub-sub-item
2. Etc.
***
### Simple tables
Column1 | Column2 | Column 3
--------|---------|---------
Row1 | |
Row2 | |
Row3 | |
***
### Simple tables cont.
Column1 | Column2 | Column 3
--------|---------|---------
Row1 | |
Row2 | |
Row3 | |
***
### Comments
<!--A comment-->
<!--(won't get rendered in any of the final output(s))-->
***
### Citations
You need a `.bib` file, which looks like this (e.g. `example.bib`):
@article{West2018,
author = {West, A.G. et al.},
year = {2018},
title = {A previous study},
journal = {Nature},
number = {50},
volume = {49},
pages = {340--346}
}
@article{West2017,
...
}
(Mendeley and other reference managing software can easily generate this file for you from your library.)
***
And link it to `analysis.Rmd` in the YAML header:
---
...
bibliography: example.bib
---
***
By adding the following heading to the end of `analysis.Rmd`:
# References
It will automatically produce the reference list!
***
Our study aligns with previous findings
[@paper1; @paper2].
***
Our study aligns with previous findings [@paper1; @paper2].
***
### R-code
We can embed figures in our document. `echo=FALSE` tells R Markdown not to display the code chunk that generates the figure.
\ ```{r, echo=FALSE}
\ plot(cars)
\ ```
(Ignore the backslashes)
***
### R-code cont.
```{r, echo=FALSE}
plot(cars)
```
***
Alternatively, we can set `echo=TRUE`:
\ ```{r, echo=TRUE}
\ x <- 1:100
\ y <- 3 * jitter(x, 100)
\ m <- lm(y ~ x)
\ visreg::visreg(m)
\ ```
***
```{r}
x <- 1:100
y <- 3 * jitter(x, 100)
m <- lm(y ~ x)
visreg::visreg(m)
```
# A _live-coding_ example!
...
# Further reading
R Markdown "Getting Started" Tutorial. <https://rmarkdown.rstudio.com/lesson-1.html>.
R Mardown Cheatsheet. <https://rmarkdown.rstudio.com/lesson-15.html>.
R Markdown Reference Guide. <https://www.rstudio.com/wp-content/uploads/2015/03/rmarkdown-reference.pdf>.
Yihui, X., Allaire, J.J., Grolemund, G. (2018). R Markdown: The Definitive Guide. <https://bookdown.org/yihui/rmarkdown/>.
# References