Chapter 4. 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 gets 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 by 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:
The script editor is a great place to put code you care about. Keep experimenting in the console, but once you have written code that works and does what you want, put it in the script editor. RStudio will automatically save the contents of the editor when you quit RStudio, and will automatically load it when you reopen. Nevertheless, it’s a good idea to save your scripts regularly and to back them up.
Running Code
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 effectively is to memorize one of the most important
keyboard shortcuts: Cmd/Ctrl-Enter. This executes the current R
expression in the console. For example, take the following code. If your
cursor is at █, pressing Cmd/Ctrl-Enter will run the complete command
that generates not_cancelled. It will also move the cursor to the next
statement (beginning with not_cancelled ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access