-
Notifications
You must be signed in to change notification settings - Fork 0
/
06-rmdIntro.Rmd
223 lines (145 loc) · 7.57 KB
/
06-rmdIntro.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
## Writing reports using R-Markdown
This page will guide you through creating and editing R-Markdown documents. This is a useful tool for reporting your analysis (e.g. for homework assignments). Of course, there is also [a cheat sheet for R-Markdown](https://www.rstudio.org/links/r_markdown_cheat_sheet)
### Creating a new R-Markdown document
0. If an R-Markdown file was provided to you, open it with R-Studio and skip to [step 4](#step4) after adding your answers.
1. Open R-Studio
2. Create a new R-Markdown document
![](./rmdExplain/start.PNG)
![](./rmdExplain/openDoc.PNG)
![](./rmdExplain/enterName.PNG)
![](./rmdExplain/template.PNG)
3. Save with appropriate name
![](./rmdExplain/saving.PNG)
3.1. Add your answers
3.2. Save again
<a name="step4"></a>
4. "Knit" to HTML
![](./rmdExplain/knit.PNG)
5. Hand in appropriate file (ending in `.html`) on learn\@WU
![](./rmdExplain/handin.PNG)
### Text and Equations
R-Markdown documents are plain text files that include both text and R-code. Using RStudio they can be converted ('knitted') to HTML or PDF files that include both the text and the results of the R-code. In fact this website is written using R-Markdown and RStudio. In order for RStudio to be able to interpret the document you have to use certain characters or combinations of characters when formatting text and including R-code to be evaluated. By default the document starts with the options for the text part. You can change the title, date, author and a few more advanced options.
![First lines of an R-Markdown document](./rmdExplain/rmdHead.PNG)
The default is text mode, meaning that lines in an Rmd document will be interpreted as text, unless specified otherwise.
#### Headings
Usually you want to include some kind of heading to structure your text. A heading is created using `#` signs. A single `#` creates a first level heading, two `##` a second level and so on.
![](./rmdExplain/headings.PNG)
It is important to note here that the ```#``` symbol means something different within the code chunks as opposed to outside of them. If you continue to put a ```#``` in front of all your regular text, it will all be interpreted as a first level heading, making your text very large.
#### Lists
Bullet point lists are created using `*`, `+` or `-`. Sub-items are created by indenting the item using 4 spaces or 2 tabs.
````
* First Item
* Second Item
+ first sub-item
- first sub-sub-item
+ second sub-item
````
* First Item
* Second Item
+ first sub-item
- first sub-sub-item
+ second sub-item
Ordered lists can be created using numbers and letters. If you need sub-sub-items use `A)` instead of `A.` on the third level.
````
1. First item
a. first sub-item
A) first sub-sub-item
b. second sub-item
2. Second item
````
1. First item
a. first sub-item
A) first sub-sub-item
b. second sub-item
2. Second item
#### Text formatting
Text can be formatted in *italics* (`*italics*`) or **bold** (`**bold**`). In addition, you can ad block quotes with `>`
````
> Lorem ipsum dolor amet chillwave lomo ramps, four loko green juice messenger bag raclette forage offal shoreditch chartreuse austin. Slow-carb poutine meggings swag blog, pop-up salvia taxidermy bushwick freegan ugh poke.
````
> Lorem ipsum dolor amet chillwave lomo ramps, four loko green juice messenger bag raclette forage offal shoreditch chartreuse austin. Slow-carb poutine meggings swag blog, pop-up salvia taxidermy bushwick freegan ugh poke.
### R-Code
R-code is contained in so called "chunks". These chunks always start with three backticks and ```r``` in curly braces (``` ```{r} ```) and end with three backticks (``` ``` ```). Optionally, parameters can be added after the ```r``` to influence how a chunk behaves. Additionally, you can also give each chunk a name. Note that these have to be **unique**, otherwise R will refuse to knit your document.
#### Global and chunk options
The first chunk always looks as follows
```{r setup, include = FALSE}`r ''`
knitr::opts_chunk$set(echo = TRUE)
```
It is added to the document automatically and sets options for all the following chunks. These options can be overwritten on a per-chunk basis.
Keep `knitr::opts_chunk$set(echo = TRUE)` to print your code to the document you will hand in. Changing it to `knitr::opts_chunk$set(echo = FALSE)` will not print your code by default. This can be changed on a per-chunk basis.
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r cars, echo = FALSE}`r ''`
summary(cars)
plot(dist~speed, cars)
```
```{r cars, echo = FALSE}
summary(cars)
plot(dist~speed, cars)
```
```{r cars2, echo = TRUE}`r ''`
summary(cars)
plot(dist~speed, cars)
```
```{r cars2, echo = TRUE}
summary(cars)
plot(dist~speed, cars)
```
A good overview of all available global/chunk options can be found [here](https://yihui.name/knitr/options/#chunk_options).
### LaTeX Math
Writing well formatted mathematical formulae is done the same way as in [LaTeX](https://en.wikipedia.org/wiki/LaTeX). Math mode is started and ended using `$$`.
````
$$
f_1(\omega) = \frac{\sigma^2}{2 \pi},\ \omega \in[-\pi, \pi]
$$
````
$$
f_1(\omega) = \frac{\sigma^2}{2 \pi},\ \omega \in[-\pi, \pi]
$$
(for those interested this is the spectral density of [white noise](https://en.wikipedia.org/wiki/White_noise))
Including inline mathematical notation is done with a single ```$``` symbol.
````
${2\over3}$ of my code is inline.
````
${2\over3}$ of my code is inline.
<br>
Take a look at [this wikibook on Mathematics in LaTeX](https://en.wikibooks.org/wiki/LaTeX/Mathematics#Symbols) and [this list of Greek letters and mathematical symbols](https://www.sharelatex.com/learn/List_of_Greek_letters_and_math_symbols) if you are not familiar with LaTeX.
In order to write multi-line equations in the same math environment, use `\\` after every line. In order to insert a space use a single `\`. To render text inside a math environment use `\text{here is the text}`. In order to align equations start with `\begin{align}` and place an `&` in each line at the point around which it should be aligned. Finally end with `\end{align}`
````
$$
\begin{align}
\text{First equation: }\ Y &= X \beta + \epsilon_y,\ \forall X \\
\text{Second equation: }\ X &= Z \gamma + \epsilon_x
\end{align}
$$
````
$$
\begin{align}
\text{First equation: }\ Y &= X \beta + \epsilon_y,\ \forall X \\
\text{Second equation: }\ X &= Z \gamma + \epsilon_x
\end{align}
$$
#### Important symbols
```{r, echo=FALSE, include=TRUE, results="asis", warning = FALSE}
library(knitr)
library(kableExtra)
lat <- readLines("./lat.txt")
lat1 <- paste0("$", lat, "$")
lat2 <- paste0("```", lat, "```")
mathy.df <- data.frame(Symbol = lat1, Code = lat2)
kable(mathy.df, escape=FALSE) %>%
kable_styling(bootstrap_options = "striped", full_width = F)
```
The `{}` after `_` and `^` are not strictly necessary if there is only one character in the sub-/superscript. However, in order to place multiple characters in the sub-/superscript they are necessary.
e.g.
```{r, echo=FALSE, include=TRUE, results="asis", warning = FALSE}
lat <- readLines("./lat2.txt")
lat1 <- paste0("$", lat, "$")
lat2 <- paste0("```", lat, "```")
mathy.df <- data.frame(Symbol = lat1, Code = lat2)
kable(mathy.df, escape=FALSE) %>%
kable_styling(bootstrap_options = "striped", full_width = F)
```
#### Greek letters
[Greek letters](https://en.wikipedia.org/wiki/Greek_alphabet#Letters) are preceded by a `\` followed by their name (`$\beta$` = $\beta$). In order to capitalize them simply capitalize the first letter of the name (`$\Gamma$` = $\Gamma$).