-
Notifications
You must be signed in to change notification settings - Fork 25
/
07-workshop-objects.Rmd
270 lines (183 loc) · 4.35 KB
/
07-workshop-objects.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
---
title: "Naming objects"
subtitle: "Session 7"
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(magick)
library(tidyverse)
library(xaringan)
library(kableExtra)
library(icons)
library(xaringanExtra)
xaringanExtra::use_panelset()
xaringanExtra::use_clipboard()
xaringanExtra::use_share_again() # need to get the slide button on html view
opts_chunk$set(
echo = TRUE,
eval = FALSE,
message = FALSE,
warning = FALSE,
fig.width = 7.252,
fig.height = 4,
dpi = 300,
dev.args = list(type = "cairo")
)
# Load data -----------------------------------
beds_data <- read_csv(url("https://raw.githubusercontent.com/nhs-r-community/intro_r_data/main/beds_data.csv"), col_types = cols(date = col_date(format = "%d/%m/%Y")),
skip = 3)
```
class: title-slide, left, bottom
# `r rmarkdown::metadata$title`
----
## **`r rmarkdown::metadata$subtitle`**
### `r rmarkdown::metadata$author`
### `r rmarkdown::metadata$date`
.right-column[
]
---
# Let's revisit our dplyr session:
</br> The mean number of beds available by date
```{r eval=TRUE}
beds_data %>%
group_by(date) %>%
summarise(mean_beds = mean(beds_av, na.rm = TRUE))
```
</br> Output = Object = New data frame!
---
class: inverse, middle, center
# Next step: Visualise
---
# Piecing it together
We can put the output...
```{r }
beds_data %>%
group_by(date) %>%
summarise(mean_beds = mean(beds_av, na.rm = TRUE))
```
</br> ... into ggplot2 code...
```{r}
ggplot(
beds_data %>% #<<
group_by(date) %>% #<<
summarise(mean_beds = mean(beds_av, na.rm = TRUE)) #<<
) +
geom_point(aes(x = date, y = mean_beds))
```
---
class: center, middle
# But, it's often better to:
## Keep wrangling separate
---
class: center, middle
# And it's <ins>always</ins> better to:
## Keep your code .green[as] readable .green[as possible]
---
# Solution:
This whole 'chunk' of code can be named
```{r}
# name =
beds_data %>%
group_by(date) %>%
summarise(mean_beds = mean(beds_av, na.rm = TRUE))
```
--
</br> With this:
```{r}
beds_ts <- beds_data %>% #<<
group_by(date) %>%
summarise(mean_beds = mean(beds_av, na.rm = TRUE))
```
---
# Good (object) names are:
</br> 1. Descriptive
</br>
</br> 2. Short.blue[(ish)]
</br>
</br> 3. Consistent .blue[with other names]
--
</br>
</br> Whilst = will work, R convention is to use the following assignment operator:
```{r eval=FALSE}
<-
```
Shortcut: <kbd> Alt - </kbd>
---
# What happens when the code is run?
The code creates an object which can be seen in the top right quadrant Environment pane
<img class="center" src="img/session07/object-creation.PNG" width="40%"/>
And this can be viewed by running the code line:
```{r}
beds_ts
```
---
# Returning to the plot
The long code used to create the dataset
```{r}
ggplot(data = beds_data %>% #<<
group_by(date) %>% #<<
summarise(mean_beds = mean(beds_av, na.rm = TRUE))) + #<<
geom_point(aes(x = date, y = mean_beds)) #<<
```
--
</br>
Becomes...
```{r}
ggplot(data = beds_ts) + #<<
geom_point(aes(x = date, y = mean_beds))
```
---
# Your turn
Give your plot a name
```{r}
# plot =
ggplot(data = beds_ts) +
geom_point(aes(x = date, y = mean_beds))
```
Then run your plot
```{r}
# plot
```
---
# Solution
```{r}
plot <- ggplot(data = beds_ts) + #<<
geom_point(aes(x = date, y = mean_beds))
plot #<<
```
---
# Naming style
The way names are written out is a question of style but it's best to be consistent.
The two common forms in R are camelCase and snake_case but others are:
```{r}
camelCase # first letter is small case
PascalCase # every letter is capital
snake_case # lower case and words are separated with underline
kebab-case # lower case and hyphen, used in RMarkdown but not R scripts
```
---
#### This work is licensed as
</br> Creative Commons
</br> Attribution
</br> ShareAlike 4.0
</br> International
</br> To view a copy of this license, visit
</br> https://creativecommons.org/licenses/by/4.0/