forked from bioinformatics-core-shared-training/r-intro
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTeachingScript_final.Rmd
More file actions
314 lines (226 loc) · 6.31 KB
/
TeachingScript_final.Rmd
File metadata and controls
314 lines (226 loc) · 6.31 KB
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
---
title: "MyFirstRmarkdown"
author: "Ashley Sawle"
date: "2th April 2025"
output:
html_document:
toc: false
toc_float: true
---
```{r setup, message=FALSE}
library(tidyverse)
library(patchwork)
library(knitr)
library(kableExtra)
library(DT)
library(readr)
opts_chunk$set(warning = FALSE, echo = TRUE, cache = TRUE)
```
## The `mtcars` data set
### Description of the data set
There is a popular built-in data set in R called "**mtcars**" (Motor Trend Car
Road Tests), which is retrieved from the 1974 Motor Trend US Magazine. The
dataset contains measurements on *11 different* attributes for *32 different*
cars.
## More inline formatting {#inline_section}
Superscript: 32 = 2^5^
Subscript: Burning petrol creates CO~2~
Code: Later we will used the `tidyverse` package
## Using tab sets {.tabset}
### Bold
This is **bold**.
It is not ~subscript~.
### Italics
This is *italics*
### Strikethrough
This is ~~strikethrough~~
## End the tabs
To end the tabs, start a new section at the same level (or higher) as before.
## A note on linebreaks
Rmarkdown does not recognise a new line as a "line break". This is the next line, but it will stay in the same line of text.
Rmarkdown does not recognise a new line as a "line break".\
To create a break add two (or more) spaces at the end of the previous line.
## Lists (bullet points)
To create an unordered list (bullet points):
* Use an asterisk as the beginning of the line
* Each asterisk is a new bullet
* You can also use "+" or "-"
- you can create sublists by indenting
- using the different symbols at different levels is clearer but not
necessary
To create an ordered list use a number, a lower case letter or "i" for roman
numerals
Some animals:
1. Cat
1. Ferret
1. Dog
a. Terriers
i. Jack Russell
ii. Airedale
b. Spaniels
48. Goldfish
## Links
Basic syntax is the link text surrounded by square brackes, followed by the link
surrounded by round brackets.
### External links
[Link to the BBC home page](https://www.bbc.co.uk)
### Internal links
These are links within the document. First we need to create a tag for
the header we want to link to by adding `{#tag_name}` after the header.
[Go to inline section](#inline_section)
## Inserting images
Similar syntax to the links, but precede it with an `!`. The first part
becomes a caption
{width=50%}
## Manual tables
| Name | Band |
| :---------: | ----------: |
| Mick | Stones |
| John | Beatles |
| Paul | Beatles |
## Blockquotes
> There is a popular built-in data set in R called "**mtcars**" (Motor Trend Car
> Road Tests), which is retrieved from the 1974 Motor Trend US Magazine. The
> dataset contains measurements on *11 different* attributes for *32 different*
> cars.
## R code chunks
They look like this
```{r}
x <- 15
y <- 20
x + y
```
## Chunk options
```{r, echo=FALSE}
x <- 19
y <- 20
x + y
```
```{r, include=FALSE}
x <- 19
y <- 90
a <- x + y
```
```{r, eval=FALSE}
x <- 19
y <- 190
a <- x + y
```
## Chunk labels
```{r label}
z <- 10
z^8
```
## The "setup" chunk
Put this at the very start of your script. It should load all necessary packages
and set any global options (see the chunk).
## Tabular output from chunk
```{r smallTable}
smallTable <- mtcars %>%
rownames_to_column("car") %>%
select(car, mpg, cyl, wt) %>%
slice(1:5)
smallTable
```
### The `kable` function
```{r}
smallTable %>%
kable(digits = 2, align = "c")
```
### The `kableExtra` package
```{r}
smallTable %>%
kbl() %>%
kable_styling(bootstrap_options = c("striped")) %>%
column_spec(column = 1, bold = TRUE, border_right = TRUE)
```
Built in "themes"
```{r}
smallTable %>%
kbl() %>%
kable_classic(full_width = F)
```
### Interactive tables with `DT`
```{r}
longTable <- mtcars %>%
rownames_to_column("car")
longTable %>%
datatable()
```
Let's:
* remove the search box
* change the number of rows displayed to 5
* add buttons to allow the table to be downloaded
* customize the column names
```{r}
longTable %>%
datatable(
extensions = "Buttons",
options = list(
dom = "Btip",
buttons = c("csv", "excel"),
pageLength = 5),
colnames = c("Car", "Miles per gallon", "Cylinders", "Weight")
)
```
```
`dom`:
* l - length changing input control (how many rows to display)
* f - filtering input (search box)
* t - table
* i - table information summary (e.g. “Showing 1 to 10 of 57 entries”)
* p - pagination control (next/previous buttons)
* B - buttons (export buttons)
* r - processing display element (loading indicator)
```
## Plots
If a code chunk generates a plot it will be displayed in the rendered document.
We can control the size of the figure and add a caption using the chunk options.
```{r plot, fig.with=5, fig.height=3, fig.cap=figCap}
figCap <- "Scatter plot of weight vs miles per gallon"
mtcars %>%
ggplot(aes(x = mpg, y = wt)) +
geom_point() +
labs(title = "Scatter plot of weight vs miles per gallon",
x = "Miles per gallon",
y = "Weight") +
theme_minimal()
```
### Combining multiple plots with `patchwork`
```{r, patchwork}
p1 <- mtcars %>%
mutate(across(cyl, factor)) %>%
ggplot(aes(x = mpg, y = wt)) +
geom_point(aes(colour = cyl)) +
geom_text(label = "A", x = 22.5, y = 3.5, size = 50)
p2 <- mtcars %>%
mutate(across(cyl, factor)) %>%
ggplot(aes(x = gear, y = hp)) +
geom_point(aes(colour = cyl)) +
geom_text(label = "B", x = 4, y = 195, size = 50)
p1 + p2
```
We can use `+`, `/` and `|` with brackets to control the layout of the plots.
We can also add annotation(e.g title, subtitle, plot labels).
We can also combine the legends.
```{r}
((p1 + p2) / (p2 + p1 + p2) | (p1 / p2 / p1)) +
plot_layout(guides = "collect") +
plot_annotation(title = "Patchwork example",
subtitle = "Combining plots with patchwork")
```
## Inline code
Finally, it's possible to include inline within markdown sections.
Useful if we can to include a value from a variable in the text.
```{r inline}
six_cyl_mean_mpg <- mtcars %>%
filter(cyl == 6) %>%
summarise(mpg = mean(mpg)) %>%
pull(mpg)
```
The mean mpg of cars with 6 cylinders was **`r six_cyl_mean_mpg`**.
## Session info
Good practice....
```{r}
devtools::session_info()
```