r4ds/workflow-scripts.Rmd

60 lines
3.2 KiB
Plaintext
Raw Normal View History

2016-08-19 03:51:47 +08:00
# Workflow: scripts
So far you've been using the console to run code. That's a great place to start, but you'll find it starts to get cramped pretty quickly as you create more complex ggplot2 graphics and dplyr pipes. To give yourself more room to work, it's a great idea to use the script editor. Open it up either clicking the File menu, and selecting New File, then R script, or using the keyboard shortcut Cmd/Ctrl + Shift + N. Now you'll see four panes:
```{r echo = FALSE, out.width = "75%"}
knitr::include_graphics("diagrams/intro-rstudio.png")
```
The script editor is a great place to put code you care about. Keep experimenting in the console, but once you get some code that does what you want, put it in the script editor.
The script editor is also a great place to build up complex ggplot2 plots or long sequences of dplyr manipulations. The key to using the script editor effective is to memorise one of the most important keyboard shortcuts: Cmd/Ctrl + Enter. This executes the current R expression in the console.
For example, take this code. If your cursor is at █, pressing Cmd + Enter will run the complete command that generates `not_cancelled`. It will also move the cursor to the next statement (beginning with `not_cancelled %>%`), which makes easy to run your script chunk by chunk.
```{r, eval = FALSE}
library(dplyr)
library(nycflights13)
not_cancelled <- flights %>%
filter(!is.na(dep_delay), !is.na(arr_delay))█
not_cancelled %>%
group_by(year, month, day) %>%
summarise(mean = mean(dep_delay))
```
You can run the complete script with one press: Cmd/Ctrl + Shift + S. Doing this regularly is a great way to check that you've captured all the important parts of your code in the script. I recommend that you always start your script with the packages that you need. That way, if you share you code with others, they can easily see what packages they need to install.
The script editor will also highlight syntax errors with a red squiggly line and a cross in the sidebar:
```{r echo = FALSE, out.width = NULL}
knitr::include_graphics("screenshots/rstudio-diagnostic.png")
```
Hover over the cross to see what the problem is:
```{r echo = FALSE, out.width = NULL}
knitr::include_graphics("screenshots/rstudio-diagnostic-tip.png")
```
RStudio will also let you know about potential problems:
```{r echo = FALSE, out.width = NULL}
knitr::include_graphics("screenshots/rstudio-diagnostic-warn.png")
```
RStudio will automatically save the contents of the editor when you quit RStudio, and will automatically load it when you re-open. Nevertheless, it's a good idea to regular save your scripts and back them up.
When working through future chapters, I highly recommend starting in the editor and practicing your the keyboard shortcuts. Over time, sending code to the console in this way will become so natural that you won't even think about it.
## Practice
1. Go to the RStudio Tips twitter account, <https://twitter.com/rstudiotips>
and find one tip that looks interesting. Practice using it!
1. What other common mistakes will RStudio diagonistcs reports? Read
<https://support.rstudio.com/hc/en-us/articles/205753617-Code-Diagnostics> to
find out.