r4ds/workflow-basics.Rmd

159 lines
5.6 KiB
Plaintext
Raw Normal View History

2016-08-19 03:51:47 +08:00
# Workflow: basics
2016-10-12 22:26:50 +08:00
You now have some experience running R code. I didn't give you many details, but you've obviously figured out the basics, or you would've thrown this book away in frustration! Frustration is natural when you start programming in R, because it is such a stickler for punctuation, and even one character out of place will cause it to complain. But while you should expect to be a little frustrated, take comfort in that it's both typical and temporary: it happens to everyone, and the only way to get over it is to keep trying.
2016-10-05 00:08:12 +08:00
Before we go any further, let's make sure you've got a solid foundation in running R code, and that you know about some of the most helpful RStudio features.
2016-08-19 03:51:47 +08:00
2016-08-20 03:42:43 +08:00
## Coding basics
Let's review some basics we've so far omitted in the interests of getting you plotting as quickly as possible. You can use R as a calculator:
2016-08-19 03:51:47 +08:00
```{r}
1 / 200 * 30
(59 + 73 + 2) / 3
2016-08-20 03:42:43 +08:00
sin(pi / 2)
2016-08-19 03:51:47 +08:00
```
2016-08-20 03:42:43 +08:00
You can create new objects with `<-`:
2016-08-19 03:51:47 +08:00
```{r}
x <- 3 * 4
```
All R statements where you create objects, __assignment__ statements, have the same form:
```{r eval = FALSE}
object_name <- value
```
2016-10-03 20:11:49 +08:00
When reading that code say "object name gets value" in your head.
2016-08-19 03:51:47 +08:00
2016-10-03 20:41:00 +08:00
You will make lots of assignments and `<-` is a pain to type. Don't be lazy and use `=`: it will work, but it will cause confusion later. Instead, use RStudio's keyboard shortcut: Alt + - (the minus sign). Notice that RStudio automagically surrounds `<-` with spaces, which is a good code formatting practice. Code is miserable to read on a good day, so giveyoureyesabreak and use spaces.
2016-08-19 03:51:47 +08:00
2016-08-20 03:42:43 +08:00
## What's in a name?
2016-08-19 03:51:47 +08:00
2016-08-20 03:42:43 +08:00
Object names must start with a letter, and can only contain letters, numbers, `_` and `.`. You want your object names to be descriptive, so you'll need a convention for multiple words. I recommend __snake_case__ where you separate lowercase words with `_`.
2016-08-19 03:51:47 +08:00
```{r, eval = FALSE}
i_use_snake_case
otherPeopleUseCamelCase
some.people.use.periods
2016-08-20 03:42:43 +08:00
And_aFew.People_RENOUNCEconvention
2016-08-19 03:51:47 +08:00
```
2016-08-20 03:42:43 +08:00
We'll come back to code style later, in [functions].
2016-08-19 03:51:47 +08:00
You can inspect an object by typing its name:
```{r}
x
```
Make another assignment:
```{r}
this_is_a_really_long_name <- 2.5
```
2016-10-03 21:36:51 +08:00
To inspect this object, try out RStudio's completion facility: type "this", press TAB, add characters until you have a unique prefix, then press return.
2016-08-19 03:51:47 +08:00
2016-10-03 21:36:51 +08:00
Ooops, you made a mistake! `this_is_a_really_long_name` should have value 3.5 not 2.5. Use another keyboard shortcut to help you fix it. Type "this" then press Cmd/Ctrl + ↑. That will list all the commands you've typed that start those letters. Use the arrow keys to navigate, then press enter to retype the command. Change 2.5 to 3.5 and rerun.
2016-08-19 03:51:47 +08:00
Make yet another assignment:
```{r}
r_rocks <- 2 ^ 3
```
Let's try to inspect it:
2016-08-20 03:42:43 +08:00
```{r, eval = FALSE}
2016-08-19 03:51:47 +08:00
r_rock
2016-08-20 03:42:43 +08:00
#> Error: object 'r_rock' not found
2016-08-19 03:51:47 +08:00
R_rocks
2016-08-20 03:42:43 +08:00
#> Error: object 'R_rocks' not found
2016-08-19 03:51:47 +08:00
```
2016-08-20 03:42:43 +08:00
There's an implied contract between you and R: it will do the tedious computation for you, but in return, you must be completely precise in your instructions. Typos matter. Case matters.
## Calling functions
2016-08-19 03:51:47 +08:00
R has a large collection of built-in functions that are called like this:
```{r eval = FALSE}
2016-10-04 05:08:44 +08:00
function_name(arg1 = val1, arg2 = val2, ...)
2016-08-19 03:51:47 +08:00
```
2016-08-20 03:42:43 +08:00
Let's try using `seq()` which makes regular **seq**uences of numbers and, while we're at it, learn more helpful features of RStudio. Type `se` and hit TAB. A popup shows you possible completions. Specify `seq()` by typing more (a "q") to disambiguate, or by using ↑/↓ arrows to select. Notice the floating tooltip that pops up, reminding you of the function's arguments and purpose. If you want more help, press F1 to get all the details in help tab in the lower right pane.
2016-08-19 03:51:47 +08:00
Press TAB once more when you've selected the function you want. RStudio will add matching opening (`(`) and closing (`)`) parentheses for you. Type the arguments `1, 10` and hit return.
```{r}
seq(1, 10)
```
Type this code and notice similar assistance help with the paired quotation marks:
```{r}
x <- "hello world"
```
Quotation marks and parentheses must always come in a pair. RStudio does its best to help you, but it's still possible to mess up and end up with a mismatch. If this happens, R will show you the continuation character "+":
2016-08-19 03:51:47 +08:00
```
> x <- "hello
+
```
The `+` tells you that R is waiting for more input; it doesn't think you're done yet. Usually that means you've forgotten either a `"` or a `)`. Either add the missing pair, or press ESCAPE to abort the expression and try again.
2016-08-19 03:51:47 +08:00
If you make an assignment, you don't get to see the value. You're then tempted to immediately double-check the result:
2016-08-19 03:51:47 +08:00
```{r}
y <- seq(1, 10, length.out = 5)
2016-08-19 03:51:47 +08:00
y
```
This common action can be shortened by surrounding the assignment with parentheses, which causes assignment and "print to screen" to happen.
```{r}
(y <- seq(1, 10, length.out = 5))
2016-08-19 03:51:47 +08:00
```
Now look at your environment in the upper right pane:
```{r, echo = FALSE, out.width = NULL}
knitr::include_graphics("screenshots/rstudio-env.png")
```
2016-08-20 03:42:43 +08:00
Here you can see all of the objects that you've created.
2016-08-19 03:51:47 +08:00
## Practice
1. Why does this code not work?
```{r, error = TRUE}
my_variable <- 10
my_varıable
```
Look carefully! (This may seem like an exercise in pointlessness, but
training your brain to notice even the tiniest difference will pay off
when programming.)
2016-10-03 20:41:00 +08:00
1. Tweak each of the following R commands so that they run correctly:
2016-08-19 03:51:47 +08:00
```{r, eval = FALSE}
library(tidyverse)
2016-08-19 03:51:47 +08:00
ggplot(dota = mpg) +
geom_point(mapping = aes(x = displ, y = hwy))
fliter(mpg, cyl = 8)
filter(diamond, carat > 3)
2016-08-19 03:51:47 +08:00
```
2016-08-20 03:42:43 +08:00
1. Press Alt + Shift + K. What happens? How can you get to the same place
using the menus?