-
Notifications
You must be signed in to change notification settings - Fork 1
/
03_RMarkdown.Rmd
160 lines (105 loc) · 4.86 KB
/
03_RMarkdown.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
---
title: "R Markdown"
author: "Gregor Pirs, Jure Demsar and Erik Strumbelj"
date: "25/7/2019"
output:
prettydoc::html_pretty:
theme: architect
toc: no
highlight: github
---
<div style="text-align:center">
<img src="bstatcomp.png" alt="drawing" width="128"/>
</div>
# RMarkdown
RMarkdown is an elegant way for writing reports or scientific papers. The best thing about RMarkdown is that all your code for data analysis and visualizations is included in the report so your work is presented in a completely transparent fashion. Another great thing is that if your data set changes or updates, you just change the data and re-knit the report. Knitting is the term used for compiling reports. The package for creating RMarkdown reports is called `rmarkdown` and is usually already installed with RStudio.
## Settings
We start by providing some basic information about our report, such as the title of the report, its date and authors. We also have to define what kind of outputs we want to create. RMarkdown can output a huge amount of document types -- pdf, html, docx, pptx, etc. For the whole list see https://rmarkdown.rstudio.com/lesson-9.html.
The code below should lie at the beginning of a RMarkdown document. In our case we will produce two output documents -- a pdf and a web page.
```
---
title: "RMarkdown example"
date: "25/7/2019"
author: "Gregor Pirs, Jure Demsar and Erik Strumbelj"
output:
pdf_document: default
html_document: default
---
```
Default designs for documents are quite dull and unpolished, luckily there are several packages that offer nicer themes. One of them is `prettydoc`.
```
output:
prettydoc::html_pretty:
theme: architect
toc: yes
highlight: github
```
## Headers
To create titles for sections and subsections we can use several levels of headers.
```
# Header 1
## Header 2
### Header 3
etc.
```
## Emphasizing
We can emphasize certain words in our text by putting them in a `rectangle`, making them **bold** or _italic_.
```
`rectangle`
**bold**
_italic_
```
## Lists
We can also create lists. Both ordered
1. Item 1,
2. Item 2,
3. Item 3
and unordered
* Item 1,
* Item 2,
* Item 3.
Below is the code to achieve this:
```
1. Item 1
2. Item 2
3. Item 3
* Item 1
* Item 2
* Item 3
```
## Code
The most powerful part of RMarkdown is the ability to incorporate executable code directly into our reports. We start and end a code block with \`\`\`. Next we have to define the programming language that will be used in the code block, we will use R, but markdown also supports Python, SQL, Bash, Rcpp, Stan, JavaScript and CSS. We define the programming language in curly brackets just after the start of the code block, for example to use R code we would use \`\`\`{r}. Besides the programming language we have a number of switches that define what will be printed out in the knitted report:
* `include = FALSE` prevents code and results from appearing in the output file. R Markdown still runs the code in the chunk, and the results can be used by other chunks.
* `echo = FALSE` prevents code, but not the results from appearing in the output file. This is a useful way to embed figures.
* `message = FALSE` prevents messages that are generated by code from appearing in the oputput file.
* `warning = FALSE` prevents warnings that are generated by code from appearing in the output file.
* `eval = FALSE` prevents execution of the code, the code is only printed out in the output file.
By default all switches described above are set to TRUE. To print only results of the code and not the code itself we would start the code chunk with \`\`\`{r, echo = FALSE}. The code below loads some data in a `data.frame` and prints out its first few rows.
```{r}
# load data
data <- read.csv("./data/temperature.csv", sep=";")
head(data)
```
We can also easily add visualizations, the image below visualizes January temperature in Slovenia through ages.
```{r, message = FALSE}
# libraries
library(dplyr)
library(ggplot2)
# filter data
january_data <- data %>% filter(country == "Slovenia" & month == 1)
ggplot(data=january_data, aes(x=year, y=temperature)) +
geom_point() +
geom_smooth()
```
## Equations
There are two ways to include equations in our report. The first one are inline equations, this is useful for simple equations, for example $y = x^2$ can be printed by typing the code
```
$y = x^2$
```
The equation are written in LaTeX code, we start and end the inline equation with a single dollar sign (\$). For superscript we use the `^` sign, and for subscript the `_` sign.
More complex equations are rendered separately from text, to achieve this type a double dollar sign (\$\$) at the beginning and at the end of an equation. We use this format for more complex equations, such as the Bayes' theorem:
$$P(A \mid B) = \frac{P(B \mid A) \, P(A)}{P(B)}.$$
The code for this equation is:
```
$$P(A \mid B) = \frac{P(B \mid A) \, P(A)}{P(B)}$$
```