2016-08-19 03:51:47 +08:00
# Workflow: basics
2021-02-21 23:40:40 +08:00
You now have some experience running R code.
We 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.
2022-05-08 10:17:21 +08:00
But while you should expect to be a little frustrated, take comfort in that this experience is 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
2021-02-21 23:40:40 +08:00
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
```
2022-05-08 10:17:21 +08:00
You can create new objects with the assignment operator `<-`:
2016-08-19 03:51:47 +08:00
```{r}
x <- 3 * 4
```
2022-02-15 00:00:53 +08:00
You can **c**ombine multiple elements into a vector with `c()`:
```{r}
primes <- c(1, 2, 3, 5, 7, 11, 13)
```
And basic arithmetic is applied to every element of the vector:
```{r}
primes * 2
primes - 1
```
2021-02-21 23:40:40 +08:00
All R statements where you create objects, **assignment** statements, have the same form:
2016-08-19 03:51:47 +08:00
2022-03-05 10:09:00 +08:00
```{r}
#| eval: false
2016-08-19 03:51:47 +08:00
object_name <- value
```
2022-05-08 10:17:21 +08:00
When reading that code, say "object name gets value" in your head.
2016-08-19 03:51:47 +08:00
2021-02-21 23:40:40 +08:00
You will make lots of assignments and `<-` is a pain to type.
2022-05-08 10:17:21 +08:00
Don't be lazy and use `=`; it will work, but it will cause confusion later.
2021-02-21 23:40:40 +08:00
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
2022-02-23 05:01:55 +08:00
## Comments
R will ignore any text after `#`.
This allows to you to write **comments**, text that is ignored by R but read by other humans.
We'll sometimes include comments in examples explaining what's happening with the code.
2022-03-05 10:09:00 +08:00
Comments can be helpful for briefly describing what the subsequent code does.
```{r}
# define primes
primes <- c(1, 2, 3, 5, 7, 11, 13)
# multiply primes by 2
primes * 2
```
With short pieces of code like this, it might not be necessary to leave a command for every single line of code.
But as the code you're writing gets more complex, comments can save you (and your collaborators) a lot of time in figuring out what was done in the code.
However, ultimately, *what* was done is possible to figure out, even if it might be tedious at times, as the code is self-documenting.
However, remembering or figuring out *why* something was done can be much more difficult, or impossible.
For example, `geom_smooth()`, which draws a smooth curve to represent the patterns of the data has an argument called `span`, which controls the "wiggliness" of the smoother with larger values for `span` yielding a smoother curve.
The default value of this argument is 0.75.
Suppose you decide to change the value of `span`, and set it to 0.3.
It would be very useful to add a comment noting why you decided to make this change, for yourself in the future and others reviewing your code.
In the following example the first comment for the same code is not as good as the second one as it doesn't say why the decision to change the span was made.
2022-02-23 05:01:55 +08:00
2016-08-20 03:42:43 +08:00
## What's in a name?
2016-08-19 03:51:47 +08:00
2021-02-21 23:40:40 +08:00
Object names must start with a letter, and can only contain letters, numbers, `_` and `.`.
2022-05-08 10:17:21 +08:00
You want your object names to be descriptive, so you'll need to adopt a convention for multiple words.
2021-02-21 23:40:40 +08:00
We 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
```
2022-03-03 23:31:46 +08:00
We'll come back to names again when we talk more about code style in Chapter \@ref(workflow-style).
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
2021-02-21 23:40:40 +08:00
Ooops, you made a mistake!
2022-05-08 10:17:21 +08:00
The value of `this_is_a_really_long_name` should be 3.5, not 2.5.
2021-02-21 23:40:40 +08:00
Use another keyboard shortcut to help you fix it.
Type "this" then press Cmd/Ctrl + ↑.
2022-05-08 10:17:21 +08:00
Doing so will list all the commands you've typed that start with those letters.
2021-02-21 23:40:40 +08:00
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
```
2022-05-08 10:17:21 +08:00
This illustrates the implied contract between you and R: R will do the tedious computations for you, but in exchange, you must be completely precise in your instructions.
2021-02-21 23:40:40 +08:00
Typos matter.
Case matters.
2016-08-20 03:42:43 +08:00
## 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
```
2022-05-08 10:17:21 +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.
2021-02-21 23:40:40 +08:00
Type `se` and hit TAB.
A popup shows you possible completions.
2021-03-29 21:58:27 +08:00
Specify `seq()` by typing more (a `q`) to disambiguate, or by using ↑/↓ arrows to select.
2021-02-21 23:40:40 +08:00
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 the help tab in the lower right pane.
2016-08-19 03:51:47 +08:00
2022-05-08 10:17:21 +08:00
When you've selected the function you want, press TAB again.
2021-02-21 23:40:40 +08:00
RStudio will add matching opening (`(`) and closing (`)`) parentheses for you.
Type the arguments `1, 10` and hit return.
2016-08-19 03:51:47 +08:00
```{r}
seq(1, 10)
```
2022-05-08 10:17:21 +08:00
Type this code and notice that RStudio provides similar assistance with the paired quotation marks:
2016-08-19 03:51:47 +08:00
```{r}
x <- "hello world"
```
2021-02-21 23:40:40 +08:00
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
2021-02-21 23:40:40 +08:00
> x <- "hello
+
2016-08-19 03:51:47 +08:00
2021-02-21 23:40:40 +08:00
The `+` tells you that R is waiting for more input; it doesn't think you're done yet.
2022-05-08 10:17:21 +08:00
Usually, this 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
2022-05-08 10:17:21 +08:00
Note that the environment tab in the upper right pane displays all of the objects that you've created:
2016-08-19 03:51:47 +08:00
```{r, echo = FALSE, out.width = NULL}
knitr::include_graphics("screenshots/rstudio-env.png")
```
2020-01-16 02:40:59 +08:00
## Exercises
2016-08-19 03:51:47 +08:00
1. Why does this code not work?
```{r, error = TRUE}
my_variable <- 10
my_varı able
```
2021-02-21 23:40:40 +08:00
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.)
2. Tweak each of the following R commands so that they run correctly:
2016-08-19 03:51:47 +08:00
```{r, eval = FALSE}
2021-04-18 22:48:54 +08:00
libary(tidyverse)
2016-10-04 20:49:10 +08:00
2020-01-16 02:37:55 +08:00
ggplot(dota = mpg) +
2021-04-18 22:48:54 +08:00
geom_point(maping = aes(x = displ, y = hwy))
2016-08-19 03:51:47 +08:00
```
2016-08-20 03:42:43 +08:00
2021-02-21 23:40:40 +08:00
3. Press Alt + Shift + K.
What happens?
How can you get to the same place using the menus?