-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy path02-workflow_basics.Rmd
231 lines (159 loc) · 6.18 KB
/
02-workflow_basics.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
# Workflow: basics
**Learning objectives:**
- Review some basic R coding.
- Follow good **style conventions** when writing code.
- Explain the importance of commenting your code.
- Recognize good practices when naming objects.
- Confidently call **functions** in R.
## Basic math calculations {-}
```{r 03-basic-math}
1 / 200 * 30
(59 + 73 + 2) / 3
sin(pi / 2)
```
## Create new objects {-}
Use the 'assignment operator' `<-`
- Windows: 'ALT -'.
- Mac: 'Option -'.
- Pronunciation: `x <- 3*4`
- "to ... assign" or "is assigned to": "To object x ***assign*** three by four" OR "three by four ***is assigned to*** object x"
- "gets": "x ***gets*** 3 times 4".
## Create new objects: Reverse assign {-}
Can go the other way `->`, eg graphs:
```{r 03-reverse-assign}
library(ggplot2)
ggplot(mtcars, aes(wt, hp)) +
geom_point() -> plot01
plot01
```
## Create new objects: Double assign {-}
- *Double assign or scoping assign `<<-`?!*
## Combining elements {-}
- Combine elements into a vector with `c()`
- Basic arithmetic operations applied to every element of a vector.
```{r 03-vector-arithmetic}
primes <- c(2, 3, 5, 7, 11, 13)
primes * 2
```
## Comments {-}
Use `#` to insert comments in R.
```{r 03-comments, eval = FALSE}
# Create vector of primes
primes <- c(2, 3)
# Multiply primes by 2
primes * 2
```
## Comments: Why? {-}
- Commenting your own code can save (future) you & and collaborators time.
- *Why* of your code, not the *how* or *what.*
- Bad: `# Changed default value to 0.9`
- Good: `# Increased smoothing parameter to better capture the trend in the data.`
## Assigning names {-}
- Names must start with a letter, contain letters, numbers, `_`, `.`.
- Good style convention ➡️ more readable code.
- Use descriptive names. Long names are ok!
- Book recommends `snake_case` (see next slide).
- R is case-sensitive! And it can't read your mind.
- Typos matter!
## Assigning names: Cases {-}
![](images/cases.png)
## Calling Functions {-}
Functions overall look like this:
```{r 03-function-signature, eval=FALSE}
function_name(argument1 = value 1,
argument2 = value 2,
...)
```
- Functions and objects are always displayed in the Environment tab in RStudio.
## Calling Functions: Arguments {-}
- TAB to show possible completions of function & arguments.
- Use ESC to abort the suggestions.
- Can usually skip arg names, but can make code more readable.
- Order of named args isn't important
- If you skip names remember the order of the defined function.
```{r 03-arg-order, eval = FALSE}
# All of these are equivalent
seq(from = 1, to = 10)
seq(to = 10, from = 1)
seq(1, 10)
#> [1] 1 2 3 4 5 6 7 8 9 10
```
## Other RStudio Features {-}
Explore on your own:
- Console: ⬆️ to see console history
- Console: type, then (ctrl/cmd)- ⬆️ to search history
- (alt/opt)-shift-k to see lots of shortcuts
- *One of my recent favorites: alt-command-⬇️.*
## Exercises
1. Why does this code not work?
```{r 03-ex-01, eval=FALSE}
my_variable <- 10
my_varıable
#> Error in eval(expr, envir, enclos): object 'my_varıable' not found
```
> Because the name on the second line doesn't include and `i`. Remember, typos matter.
2. Tweak each of the following R commands so that they run correctly:
```{r 03-ex-02, eval = FALSE}
libary(todyverse)
ggplot(dTA = mpg) +
geom_point(maping = aes(x = displ y = hwy)) +
geom_smooth(method = "lm)
```
> Here is the correct code with no typos:
```{r 03-ex-02b, eval=FALSE}
library(tidyverse)
ggplot(data = mpg)+
geom_point(mapping = aes(x = display, y = hwy)) +
geom_smooth(method = "lm")
```
3. Press Option + Shift + K / Alt + Shift + K. What happens? How can you get to the same place using the menus?
> This takes you to the list of shortcuts. Another way of getting here is: Tools > Keyboard shortcuts help.
4. Let’s revisit an exercise from the Section 2.6. Run the following lines of code. Which of the two plots is saved as mpg-plot.png? Why?
```{r 03-ex-04, eval=FALSE}
my_bar_plot <- ggplot(mpg, aes(x = class)) +
geom_bar()
my_scatter_plot <- ggplot(mpg, aes(x = cty, y = hwy)) +
geom_point()
ggsave(filename = "mpg-plot.png", plot = my_bar_plot)
```
> The arguments for ggsave are (`?ggsave` in the console): filename, plot, device, path, ...). Plot refers to the plot to be saved, so in this case the first plot named `my_bar_plot` will be saved as mpg-plot.png.
## Meeting Videos {-}
### Cohort 5
`r knitr::include_url("https://www.youtube.com/embed/nQnMm6kyJbE")`
<details>
<summary>Meeting chat log</summary>
```
00:14:44 Jon Harmon (jonthegeek): Ignore the URL on these, I should have made my coworker crop those out since it isn't relevant outside of our work environment 🙃
00:47:00 Jon Harmon (jonthegeek): ?variable.names
00:58:53 Becki R. (she/her): jon_doe is an object?
00:59:05 Wai-Yin: Yes.
01:10:33 Becki R. (she/her): Is there a list of verbal substitutions like "<- = get"?
01:16:05 Jon Harmon (jonthegeek): Not yet! Some more will come up as we go through the book... and I plan on putting something together with them before too long!
01:16:56 Becki R. (she/her): Nice!
01:20:44 [email protected]: Thanks
01:22:41 Becki R. (she/her): Thanks everyone! I will not be here next week.
```
</details>
### Cohort 7
`r knitr::include_url("https://www.youtube.com/embed/JDOQDfpYBmY")`
<details>
<summary>Meeting chat log</summary>
```
00:29:38 Dolleen Osundwa: is a tibble similar to the table () function?
```
</details>
### Cohort 8
`r knitr::include_url("https://www.youtube.com/embed/FeSYfRx2esU")`
<details>
<summary>Meeting chat log</summary>
```
00:06:13 Shamsuddeen Muhammad: book_club-py4da
00:06:16 Shamsuddeen Muhammad: #book_club-py4da
00:08:47 Shamsuddeen Muhammad: https://www.youtube.com/watch?v=8tq1C8spV_g
00:09:03 Abdou Daffeh: Hey everyone
00:34:25 Abdou Daffeh: sorry do you mean vscode?
00:34:45 Hamza: Yes he is using VSCode
00:35:13 Abdou Daffeh: thanks, I have to check it out
00:41:22 Shamsuddeen Muhammad: https://www.rscreencasts.com/content_pages/hbcu-enrollment.html
```
</details>