-
Notifications
You must be signed in to change notification settings - Fork 1
/
03_functions.Rmd
167 lines (113 loc) · 3 KB
/
03_functions.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
---
title: "Functions"
subtitle: "Part 3 of the RStudio 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>
```{r packages, message=FALSE, warning=FALSE, include=FALSE}
library(palmerpenguins)
library(tidyverse)
library(janitor)
library(here)
```
# Review
- we made a new function - clean_if_bad_names()
- shift+cmd+p for command palette
- fun/if/for/lib/ec then shift+tab to add snippets
- esquisse addin to generate plot code if new to ggplot2
- alt+dash for assignment arrow
- cmd+up in console to see history
# Functions - let's inspect!
```{r }
clean_if_bad_names <- function(x) {
if (any(str_detect(names(x), "[\\sA-Z()/-]"))) {
x <- clean_names(x)
x
}
x
}
# reset df
df <- penguins_raw
clean_if_bad_names(x = df)
```
<br>
# But we don't really need that silly function
clean_names() from the janitor package looks for more bad elements in names\
How do we know?\
`r emo::ji("smile")` Pro tip: cmd+click or F2 on a function name to inspect it's body
```{r echo=TRUE, eval=FALSE}
janitor::clean_names()
```
```{r}
# we don't really need to test first, just clean the column names
df <- penguins_raw %>% janitor::clean_names()
```
![](images/inspect1.png)\
Select the data.frame method from the drop down ![](images/inspect2.png)
Then cmd click the internal function make_clean_names()\
![](images/inspect3.png)
Now we see the internals of janitor's make_clean_names(), we see it cleans up apostrophes and other things our function didn't test for.\
![](images/inspect4.png)
<br>
# What other functions do you want to inspect?
```{r echo=TRUE, eval=FALSE}
dplyr::arrange()
```
<br>
## Water cooler chat: Chunk options in gear icon
In the chunk above, the options in the gear icon do not provide a way for:\
`{r echo=TRUE, eval=FALSE}`\
which means to show the code but do not run the code.
<br>
## Function and snippet mashup
`r emo::ji("smile")` combine some functions into one function using purrr::compose()
```{r}
not_in <- purrr::compose( # by default it runs bottom to top
`!`,
`%in%`
)
```
```{r}
not_in(3, c(1:2))
not_in(1, c(1:2))
not_in("January", month.name)
```
```{r compose, message=FALSE, warning=FALSE}
library(broom)
library(gt)
lm_peek <- purrr::compose(
.dir = "forward",
lm,
broom::tidy,
gt::gt,
~tab_header(.x, title = "Results Preview")
)
# this
lm_peek(culmen_depth_mm ~ flipper_length_mm + body_mass_g, data = df)
```
<br>
## Got code?
`r emo::ji("smile")` Pro tip: highlight code, then in Code menu, select extract function
```{r echo=TRUE, eval=FALSE}
# in the code menu, select extract function
df %>%
filter(Sex == "MALE") %>%
summarise(mean_body_mass = mean(`Body Mass (g)`, na.rm = TRUE))
```