-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path02_workflow_basics.qmd
More file actions
177 lines (112 loc) · 4.7 KB
/
02_workflow_basics.qmd
File metadata and controls
177 lines (112 loc) · 4.7 KB
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
---
title: "Workflow basics"
author: "Max Hachemeister"
date: last-modified
date-format: iso
format: gfm
toc: true
toc-depth: 3
---
> "Frustration is natural when you start programming in R [...]."
## 2.1 Coding basics
Basic math calculations:
```{r}
1 / 200 * 30
(59 + 73 + 2) / 3
sin(pi / 2)
```
Create objects with the assignment operator `<-`:
```{r}
x <- 3 * 4
x
```
*C*ombine values to a vektor with `c()`:
```{r}
primes <- c(2, 3, 5, 7, 11, 13)
primes
```
And do arithmetic on such a vector:
```{r}
primes * 2
primes - 1
```
## 2.2 Comments
Use `#` to write comments into your code chunks.
Use comments to explain the why of certain lines, or arguments or values. The *why* is more important than explaining *what* the code does.
## 2.3 What's in a name?
"snake_case" is recommended over "camelCase" or "separating.periods".
R is case sensitive, which means that it will differentiate between "Lasagna_is_food" and "lasagna_is_food".
So be aware of this when naming objects and calling functions and their arguments.
## 2.4 Calling functions
>> In the explanation for `seq()`, the step of putting a colon between `from = 1` and `to = 10` is not mentioned.
You can spare some keystrokes if you know the sequence of the first few arguments of a function (which are usually the most commonly used)
So this code:
```{r}
seq(from = 1, to = 10)
```
Is internally the same as this:
```{r}
seq(1, 10)
```
Quotation marks and parentheses need to always come in pairs.
This code does not work:
```{r}
#| error: true
x <- "Hello world
```
And R will show you with a `+` in the console that it's expecting more input. Ah yeah and the chunk output will also let you know.
## 2.5 Exercises
### 1.Why does this code not work?
Example code:
```{r}
#| error: true
my_variable <- 10
my_varıable
```
The "i" in the second `my_varıable` is different than that in the assigned variable.
### 2. Tweak each of the following R commands so that they run correctly
Example code:
```{r}
#| eval: false
libary(todyverse)
ggplot(dTA = mpg) +
geom_point(maping = aes(x = displ y = hwy)) +
geom_smooth(method = "lm)
```
There is an "r" missing in "libary" and "todyverse" needs the "o" replaced with an "i".
In `ggplot` the "dTA" should be "data", while in `geom_point` the "maping" misses another "p" and the `aes` arguments should be separated with a colon.
Lastly in `geom_smooth` the value for the `method` argument is of type character and therefore needs to be surrounded by '"'.
In total the working code is this:
```{r}
#| error: true
library(tidyverse)
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy)) +
geom_smooth(method = "lm")
```
Okay, so far we've corrected all the "spelling" errors, however some parts of the code also need to be put in another place.
The error states that `geom_smooth()` requires `x` and `y` aesthetics.
They have been given in `geom_point()`, but as such only apply to this layer in particular. So to make the code work the `aes(x = displ, y = hwy)` should wander from `geom_point` to `ggplot` so that this mapping applies to all layers of the plot.
Like so:
```{r}
library(tidyverse)
ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) +
geom_point() +
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?
Pressing "Alt + Shift + K" opens the "**K**eyboard Shortcut Quick Reference". Which shows commonly used shortcuts for the RStudio IDE and also provides a link to all shortcuts in its top right corner.
You can also get to this Window by clicking "Tools" -> "Keyboard Shortcuts Help" in the menu bar on top of the Rstudio IDE.
### 4. Let's revisit an exercise from Section 1.6. Run the following lines of code. Which of the two plots is saved as `02_mpg-plot.png`? Why?
>> I added a "02" to the plot name to have the files ordered in my project folder.
Example code:
```{r}
my_bar_plot <- ggplot(mpg, aes(x = class)) +
geom_bar()
my_scatter_plot <- ggplot(mpg, aes(x = cty, y = hwy)) +
geom_point()
ggsave(filename = "02_mpg-plot.png", plot = my_bar_plot)
```
By default `ggsave()` saves the most recently drawn plot, which would've been `my_scatter_plot`, but the plot named `my_bar_plot` will get saved as `02_mpg-plot.png`, as it has explicitly been defined as an argument in `ggsave()`.
## 2.6 Summary
In this Chapter I've learned about R's more basic functionality like mathematical calculations, vectors and objects. I further got introduced to the how and why of comments in code, as well as naming conventions for files, objects, etc.. Finally it was explained how functions interpret code, so that the written code can be simplified for writing it faster and more efficient as a human.