-
Notifications
You must be signed in to change notification settings - Fork 230
/
workflow-basics.Rmd
123 lines (88 loc) · 3.8 KB
/
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
---
output: html_document
editor_options:
chunk_output_type: console
---
# Workflow: basics {#workflow-basics .r4ds-section}
```{r message=FALSE,cache=FALSE}
library("tidyverse")
```
## Exercise 4.1 {.unnumbered .exercise data-number="4.1"}
<div class="question">
Why does this code not work?
```{r error=TRUE}
my_variable <- 10
my_varıable
```
</div>
<div class="answer">
The variable being printed is `my_varıable`, not `my_variable`:
the seventh character is "ı" ("[LATIN SMALL LETTER DOTLESS I](https://en.wikipedia.org/wiki/Dotted_and_dotless_I)"), not "i".
While it wouldn't have helped much in this case, the importance of
distinguishing characters in code is reasons why fonts which clearly
distinguish similar characters are preferred in programming.
It is especially important to distinguish between two sets of similar looking characters:
- the numeral zero (0), the Latin small letter O (o), and the Latin capital letter O (O),
- the numeral one (1), the Latin small letter I (i), the Latin capital letter I (I), and Latin small letter L (l).
In these fonts, zero and the Latin letter O are often distinguished by using a glyph for zero that uses either a dot in the interior or a slash through it.
Some examples of fonts with dotted or slashed zero glyphs are Consolas, Deja Vu Sans Mono, Monaco, Menlo, [Source Sans Pro](https://adobe-fonts.github.io/source-sans-pro/), and FiraCode.
Error messages of the form `"object '...' not found"` mean exactly what they say.
R cannot find an object with that name.
Unfortunately, the error does not tell you why that object cannot be found, because R does not know the reason that the object does not exist.
The most common scenarios in which I encounter this error message are
1. I forgot to create the object, or an error prevented the object from being created.
1. I made a typo in the object's name, either when using it or when I created it (as in the example above), or I forgot what I had originally named it.
If you find yourself often writing the wrong name for an object,
it is a good indication that the original name was not a good one.
1. I forgot to load the package that contains the object using `library()`.
</div>
## Exercise 4.2 {.unnumbered .exercise data-number="4.2"}
<div class="question">
Tweak each of the following R commands so that they run correctly:
```{r, eval = FALSE}
ggplot(dota = mpg) +
geom_point(mapping = aes(x = displ, y = hwy))
fliter(mpg, cyl = 8)
filter(diamond, carat > 3)
```
</div>
<div class="answer">
```{r error=TRUE}
ggplot(dota = mpg) +
geom_point(mapping = aes(x = displ, y = hwy))
```
The error message is `argument "data" is missing, with no default`.
This error is a result of a typo, `dota` instead of `data`.
```{r error=TRUE}
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy))
```
```{r error=TRUE}
fliter(mpg, cyl = 8)
```
R could not find the function `fliter()` because we made a typo: `fliter` instead of `filter`.
```{r error=TRUE}
filter(mpg, cyl = 8)
```
We aren't done yet. But the error message gives a suggestion. Let's follow it.
```{r error=TRUE}
filter(mpg, cyl == 8)
```
```{r error=TRUE}
filter(diamond, carat > 3)
```
R says it can't find the object `diamond`.
This is a typo; the data frame is named `diamonds`.
```{r error=TRUE}
filter(diamonds, carat > 3)
```
How did I know? I started typing in `diamond` and RStudio completed it to `diamonds`.
Since `diamonds` includes the variable `carat` and the code works, that appears to have been the problem.
</div>
## Exercise 4.3 {.unnumbered .exercise data-number="4.3"}
<div class="question">
Press *Alt + Shift + K*. What happens? How can you get to the same place using the menus?
</div>
<div class="answer">
This gives a menu with keyboard shortcuts. This can be found in the menu under `Tools -> Keyboard Shortcuts Help`.
</div>