-
Notifications
You must be signed in to change notification settings - Fork 1
/
02_snippets.Rmd
265 lines (162 loc) · 4.69 KB
/
02_snippets.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
---
title: "Snippets"
subtitle: "Part 2 of the RStdio Tips and Tricks Series"
author: "Jeremy Allen"
date: "Nov 2, 2021, updated on `r Sys.Date()`"
output:
html_document:
highlight: zenburn
theme:
bg: "#303436"
fg: "#9da9af"
primary: "#ff8080"
base_font:
google: "Roboto"
code_font:
google: "JetBrains Mono"
editor_options:
chunk_output_type: console
self_contained: true
---
------------------------------------------------------------------------
<br>\
<br>
# Review
- shift+cmd+p for command palette
- shift+cmd+m for pipe
- alt-dash for assignment
- cmd+, for global options
- how do we fold code?
<br>
# Snippets! How do they work?
First, let's prep some data
```{r packages, message=FALSE, warning=FALSE, include=FALSE}
library(palmerpenguins)
library(tidyverse)
library(janitor)
library(here)
```
<br>
## Get data from the palmerpenguins package and inspect names
```{r}
df <- penguins_raw
df
# gross!
names(df)
# clean those names
df %>%
clean_names() %>%
names()
```
<br>
## How about those column names?
A little regex first\
`r emo::ji("smile")` Pro tip: match anything that you put in []
```{r}
# are there spaces or capital letters in col names?
str_detect(names(df), "[\\sA-Z()/-]")
# but we only want one answer, so wrap in any()
any(str_detect(names(df), "[\\sA-Z()/-]"))
```
`r emo::ji("smile")` Pro tip: str_view() to see string matches (requires htmlwidgets)
```{r}
# let's see where our pattern matches
str_view_all(names(df), "[\\sA-Z()/-]")
```
<br>
# Finally, a snippet!
`r emo::ji("smile")` Pro tip: shift+tab to expand a snippet
`r emo::ji("smile")` Pro tip: cmd+i to fix bad indentation
```{r}
# snippet time! type "if" then hit shift+tab
# paste in our regex condition and our clean_names code
# try yours right below here:
# should look like this when you're done
if (any(str_detect(names(df), "[\\sA-Z()/-]"))) {
df <- df %>%
clean_names()
}
# highlight the if statement above then hit cmd+i to fix the indentation
# inspect
names(df)
```
<br>
## What will this return now? Someone tell me before running it!
```{r}
any(str_detect(names(df), "[\\sA-Z()/-]"))
```
<br>
## Can we pipe that? Yes we can. Inside to outside.
`r emo::ji("smile")` Pro tip: cmd+shift+m to insert pipe
```{r}
names(df) %>%
str_detect("[\\sA-Z()/-]") %>%
any()
```
<br>
## Assign the clean names
`r emo::ji("smile")` Pro tip: alt+dash for assignment arrow
```{r echo=TRUE, eval=FALSE}
df <- df %>%
clean_names()
```
<br>
## More snippets! Functions
`r emo::ji("smile")` Pro tip: type fun then hit shift+tab\
`r emo::ji("smile")` Pro tip: cmd+f for find (and replace)
```{r echo=TRUE, eval=FALSE}
# type fun then hit shift+tab
# name it clean_if_bad_names
# one arg called x
# put our if statement in the body
# cmd+i to fix indent
# cmd+f to change df to x
# try yours right below here:
# should look like this when you are done
clean_if_bad_names <- function(x) {
if (any(str_detect(names(x), "[\\sA-Z()/-]"))) {
x <- clean_names(x)
x
}
x
}
# reset our df back to original with bad column names
df <- penguins_raw
# use our new function
df <- clean_if_bad_names(x = df)
```
<br>
## More snippets! For loops
```{r echo=TRUE, eval=FALSE}
# type for then hit shift+tab:
```
<br>
## More snippets! See them all
`r emo::ji("smile")` Pro tip: make your own
![](images/snippets1.png)
![](images/snippets2.png)
The contents of the snippet should be indented below using the tab key (rather than with spaces). Variables can be defined using the form {1:varname}.
Make a snippet called ec with the following lines
library(here)
library(ggplot2)
library(tidyr)
library(dplyr)
library(stringr)
library(purrr)
![New Snippet](images/ec_snippet.png "New snippet in the snippet editor pane")\
<br>
**Everyday Carry** - hit shift+tab after the ec below
```{r echo=TRUE, eval=FALSE}
ec
```
I use that as lightweight version of library(tidyverse) when I don't want or need to load all core tidyverse packages - particularly important for a production environment that needs to be trim.
<br>
## ggplot2 GUI plot builder will provide code
`r emo::ji("smile")` Pro tip: If you are new to ggplot2, you can install the esquisse package, which also installs the ggplot2 builder addin, which you can use to build a plot using a GUI, then have the code, too!
![](images/esqui0.gif)
<br>
### Before you start, click in the code chunk below, then go to the Addins and build your plot. When you click to insert code it will be inserted where you clicked last in your script
![](images/esqui3.gif)
```{r echo=TRUE, message=FALSE, warning=FALSE}
# click below here before starting the esquisse addin
```