-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy path01-workshop_intro.Rmd
506 lines (305 loc) · 9.73 KB
/
01-workshop_intro.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
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
---
title: "Introduction to R and Rstudio"
subtitle: "Session 1"
institute: "NHS-R Community"
output:
xaringan::moon_reader:
css:
- default
- css/nhsr.css
- css/nhsr-fonts.css
lib_dir: libs
seal: false
self_contained: true
nature:
highlightStyle: googlecode
highlightLines: true
highlightLanguage: ["r"]
countIncrementalSlides: false
ratio: "16:9"
includes:
after_body: [css/insert-logo.html]
---
```{r libs, include=FALSE}
library(knitr)
library(tidyverse)
library(xaringan)
library(magick)
library(icons)
library(xaringanExtra)
xaringanExtra::use_share_again() # need to get the slide button on html view
opts_chunk$set(
echo = FALSE,
eval = FALSE,
message = FALSE,
warning = FALSE,
fig.width = 7.252,
fig.height = 4,
dpi = 300,
dev.args = list(type = "cairo")
)
```
class: title-slide, left, bottom
# `r rmarkdown::metadata$title`
----
## **`r rmarkdown::metadata$subtitle`**
### `r rmarkdown::metadata$author`
### `r rmarkdown::metadata$date`
---
class: center, middle
# Agenda
Using R Studio
Importing data
--------- Break ---------
Introduction to ggplot2
What does this function do?
--------- Lunch -----------
Data wrangling with dplyr
Naming objects | Relational data
--------- Break ---------
R Markdown
Ongoing learning
.green[Finish about 4 - 430pm]
---
class: center, middle
# Course Aims
####1. To show you some of the possibilities:
####2. To give you a feel for how R works.
####3. To show you enough for you to begin teaching yourself
.blue[(Excellent free resources available)]
---
class: inverse, middle, center
.left-col[.center[
### elegant
# Graphics
]]
---
[<img class="center" src="img/session01/heatmap_colin_angus.PNG"/>](https://github.com/VictimOfMaths/COVID-19)
---
The original blog is no longer available that featured this
<img class="center" src="img/session01/london_cycle_routes.PNG" width="90%"/>
---
class: center, middle
# Collaboration
`r icons::fontawesome("arrow-down")`
## Reproducibility
`r icons::fontawesome("arrow-down")`
### R Markdown
---
<img class="center" src="img/session01/automated_reports.PNG"/>
---
# (Interactive) Dashboards
[Mental Health Surge Modelling](https://strategyunit.shinyapps.io/MH_Surge_Modelling/)
<img src="img/session01/mental_health_modelling.PNG"/>
---
[Trafford Data Lab](https://trafforddatalab.shinyapps.io/trafford-tweet-dash/) and the main [site](https://www.trafforddatalab.io/)
<img src="img/session01/twitter_dash.PNG"/>
---
# R to SQL connection
[NHS-R Community Webinar](https://nhsrcommunity.com/learn-r/workshops/database-connections-in-r-webinar)
<img src="img/session01/webinar_nhsr.PNG"/>
---
class: center, middle
# Inclusivity
```{r inclusive, eval=TRUE}
rladies <- magick::image_read('img/session01/r_ladies_twitter.PNG')
mir <- magick::image_read('img/session01/mir_twitter.PNG')
nhsr <- magick::image_read('img/session01/nhsr_twitter.PNG')
img <- c(rladies, mir, nhsr)
magick::image_animate(image_scale(img, "600x600"), fps = 1, dispose = "previous", loop = 6)
```
---
class: inverse, middle, center
.left-col[.center[
## Course Philosophy
]]
---
[Minimum Viable Product](https://blog.crisp.se/2016/01/25/henrikkniberg/making-sense-of-mvp)
.left-col[.center[
<img src="img/session01/mvp.PNG" width="75%"/>
]]
---
class: center, middle
# Course philosophy
Relaxed and informal
Slides and code are available on [`r icons::fontawesome("github")` GitHub](https://github.com/nhs-r-community/intro_r)
The truth; but it can’t be the whole truth…
too much to cover in a day
---
class: inverse, middle, center
# Let's begin
---
class: center, middle
## R vs. RStudio
R is a programming language
RStudio is a software application with tools to
improve your programming experience
---
<img class="center" src="img/session01/race_car.PNG"/>
---
<img class="center" src="img/session01/roberto-nickson-unsplash.PNG"/>
---
# RStudio
Many excellent features to help you with your analyses.
Never again have to think about R and RStudio as separate:
Opening R Studio opens an R session.
Analogy from the book Modern Dive:
[www.moderndive.com](https://moderndive.com/)
---
class: center, middle
# Open RStudio
R Studio opens an R session
---
.left-column[
The Console is your window to R.
You can code directly in the console…
pi*2 <kbd> Enter </kbd>
37/12 <kbd> Enter <kbd>
… but there is a better way…
]
.right-column[
<img src="img/session01/rstudio_console.PNG"/>
]
---
.left-column[
#### The Editor
If you don’t see the Editor pane, click top right button
And choose "R Script" from the drop down.
Or, shortcut:
<kbd> Ctrl + Shift + N </kbd>
.blue[The cloud shortcut is <kbd> Ctrl + Shift + Alt + N </kbd>]
]
.pull-right[
<img src="img/session01/new_file.PNG"/>
]
---
.left-column[
The Editor is just like any other text
editor: you can copy, paste, and save text.
<kbd> Ctrl + Z </kbd> undoes *but*
<kbd> Ctrl + Shift + Z </kbd> to redo
(.blue[not <kbd> Ctrl + y</kbd>])
Different text is coloured
(the console is uniform)
Autocomplete
<kbd>Ctrl + Enter </kbd>
(sends line of
code to Console)
]
.right-column[
<img src="img/session01/editor.PNG"/>
]
---
.left-column[
Comment code with a #
E.g # this was a bad idea
Comment frequently, at least in the beginning
]
.right-column[
<img src="img/session01/editor.PNG"/>
]
---
# Tools -> Options
[Reasons why this is default](https://community.rstudio.com/t/defaults-of-saving-and-restoring-workspace/939)
<img class="center" src="img/session01/global_options.PNG"/>
---
# Tools -> Options
Accessbility and comfort for all
<img class="center" src="img/session01/appearance.PNG"/>
---
class: center, middle
# Packages
### R packages are like apps for your phone:
--
Extend the capabilities of the basic or "base R"
with extra functions, datasets, documentation.
---
<img class="center" src="img/session01/app_analogy.PNG"/>
---
# Packages
Quotations in R can be either "" or '' but cannot be mixed:
```{r echo=TRUE}
# either
install.packages("tidyverse")
# or
install.packages('tidyverse')
```
will download a package to your personal library. Then:
```{r echo=TRUE}
library(tidyverse)
```
This tells R to load the package from your personal library and is .blue[needed for every new session/script]
---
class: center, middle
## CRAN repository
[Comprehensive R Archive Network](https://cran.r-project.org/web/packages/)
18,903 packages (April 2022). Free. Peer reviewed.
(Manifold possibilities) eg. interactive graphics and
dashboards, machine learning, mine twitter data,
create PowerPoint docs, maps…
## GitHub
Many useful packages in development or subject to a lot of change are not on CRAN and are available through GitHub including [{NHSRtheme}](https://github.com/nhs-r-community/NHSRtheme). These won't be peer reviewed.
## ROpenSci
ROpenSci offer a peer reviewed ecosystem of R packages through GitHub including UKHSA's [{fingertipsR}](https://github.com/ropensci/fingertipsR)
---
<img class="center" src="img/session01/tidyverse.PNG"/>
---
class: center, middle
## What is the tidyverse?
The [tidyverse package](https://www.tidyverse.org/) collects (some of) the most popular
R packages into one.
All have the same underlying principles:
Provide simple tools (with consistent structure) which may
be used together to help solve complex problems.
---
class: center, middle
## What is the tidyverse?
During the workshop we will use the [ggplot2](https://ggplot2.tidyverse.org/), [dplyr](https://dplyr.tidyverse.org/),
and [readr](https://readr.tidyverse.org/) packages. These are bundled up in the
tidyverse package. Load it by running:
```{r, echo=TRUE}
library(tidyverse)
```
---
# Output
Information - what was loaded and potential conflicts
```
-- Attaching packages ------------------------------------------ tidyverse 1.3.0 --
v ggplot2 3.3.3 v purrr 0.3.4
v tibble 3.1.0 v dplyr 1.0.4
v tidyr 1.1.2 v stringr 1.4.0
v readr 1.4.0 v forcats 0.5.1
-- Conflicts --------------------------------------------- tidyverse_conflicts() --
x dplyr::filter() masks stats::filter()
x dplyr::lag() masks stats::lag()
```
Warnings - not errors, just information on versions
```
Warning messages:
1: package ‘tidyverse’ was built under R version 4.0.4
2: package ‘tidyr’ was built under R version 4.0.3
3: package ‘readr’ was built under R version 4.0.3
4: package ‘purrr’ was built under R version 4.0.3
5: package ‘dplyr’ was built under R version 4.0.3
6: package ‘stringr’ was built under R version 4.0.3
```
---
#### This work is licensed as
Creative Commons
Attribution
ShareAlike 4.0
International
To view a copy of this license, visit
https://creativecommons.org/licenses/by/4.0/
---
name: goodbye
class: middle, inverse
# **Thank you!**
Acknowledgements: for creating the original training slides and delivering training:
Andrew Jones | Ozayr Mohammed
Healthcare Analysts | The Strategy Unit
[`r icons::fontawesome("paper-plane")` [email protected]](mailto:[email protected])
[`r icons::fontawesome("paper-plane")` [email protected]](mailto:[email protected])
And to Silvia Canelón who created the Xaringan presentation using NHS and NHR-R colour branding and shared this at the 2020 [NHS-R Community conference](https://spcanelon.github.io/xaringan-basics-and-beyond/index.html).
Details of the workshops she ran at the [NHS-R Community conference](https://spcanelon.github.io/xaringan-basics-and-beyond/index.html).
And to Zoë Turner who converted the presentation to Xaringan. [`r icons::fontawesome("paper-plane")` [email protected]](mailto:[email protected])