Simple R Scripts
1 Options for executing R commands
The analyst can execute R commands in at least three basic ways:
- Direct execution: You should already be familiar with this; if not, you should go to this page. This approach can quickly become tiresome and/or counterproductive if you’re trying to execute a more complicated series of R commands.
- Simple scripting: This leads us to the second approach (covered on this page) — writing code in a simple scripting file. This allows for much simpler editing and iteration of commands and command sequences. It also simplifies the documentation process and ability to run previously-created sequences of commands.
- Formatted reports: The third means of executing R commands is through creation of formatted reports (which are covered on this page). This approach is quite powerful, allowing the analyst to use an easy-to-learn document description language to create reports as diverse as HTML pages, PDF files, presentation files, interactive web pages with embedded controls, and whole Web sites. (In fact, this site is created with these tools.)
Let’s get on with introducing direct execution of R commands within the Console
. We will follow that up with a more in-depth discussion of simple scripting.
2 Simple scripting
As pointed out above, while working in the Console
has its uses (executing short and simple commands), an analyst quickly runs into situations that call for a more robust solution — that is, the ability to easily edit and run sequences of commands. This is the situation for which R scripts were developed.
An R script file is not much more than a series of R commands that are executed by R in the order they appear in the file.
We introduce them on this page. In the first section, we cover the basics of working with scripts. We follow this up with a description of how you can use this tool most efficiently and effectively.
2.1 The basics of working with scripts
In this section, we go through the process of creating your first script. We call ours exampler
; you can call yours whatever you want.
2.1.1 Create a script
Creating an R script is a simple process. First, click on the “New file” button in the upper left of the R Studio interface:
Then choose “R Script” from the popup menu:
After you do so, a blank, unnamed file will appear in the upper left panel.
It’s as simple as that.
2.1.2 Entering commands into a script
Type the following command into the blank file:
Note that you don’t enter a prompt and you don’t enter a line number. Just type in the command.
Also note that this is how you enter any command into a script. Just type it in. Type in more than one if you want. Just be sure to save the file periodically.
Speaking of…
2.1.3 Save a script
After entering some text into the file, you’ll want to save it.
However, before you save it, we encourage you to set up “Source on save”. This is programmer-ese that means “save the file and then execute its contents” (as a way of ensuring that your commands all work).
Doing this will add some time to your workflow (the amount of time it takes to execute the commands) but it helps you catch any errors that you add to the file as soon as possible. Most of the time we think that trade-off is worth it.
Sometimes it is worth making this change; sometimes it isn’t. We recommend that you start out turning this setting on for each file you work with. You can turn it off once you encounter a situation in which it doesn’t make sense (such as adding commands that take an extraordinarily long time to execute).
You can see the check box in this figure; check it to turn the setting on:
Now save the file by pressing Ctrl-S
(on Windows or Linux) or Cmd-S
(on a Mac); as noted above, we named our script exampler
. (R Studio will add the appropriate file extension to the script — in this case, .R
.) Save it in the appropriate directory — your current working directory would be a good choice.
After you have saved the file and given it a name, future saves will merely involve pressing the appropriate key combination needed to save a file. If you checked the checkbox as directed above, this keypress will also execute the commands in the file.
2.1.4 Adding comments to a script
Comments are text within a script file that R ignores (i.e., does not attempt to execute). These are signified within a script by the hash (#)
symbol. Everything to the right of it is the comment.
Add the following to your sample script, and then save it when you’re done:
Save this script. If you clicked Source on save
, then this script will execute. And the effects of the script should not change at all since you only added comments to the file.
You should get in the habit of putting comments within your script files. You will generally spend more time reading your scripts than you will executing your scripts; thus, simplifying the reading process should be a goal of yours. Further, you will frequently — this is actually the whole point of using R and working this way! — refer back to scripts that you have previously written with the desire to run it again and possibly revise it. Clarifying what the script actually does while it is fresh in your mind is a good way to ensure that you make this process as error-free as possible.
2.1.5 Execute a script
Now, if you clicked Source on save
above, then you have already executed your script many times. However, other ways are available for you to execute all or part of a script.
Executing a single line of the script: Place your cursor on the line in the script that you want to execute, and then press Ctrl-Enter
. Notice that after you execute that line, the cursor advances to the next line. This enables you to “step through” the whole script one line at a time.
Executing multiple lines of the script: Highlight the commands that you want to execute, and then press Ctrl-Enter
or Cmd-Return
.
Executing the whole script: If you want to execute the whole script file, then press Ctrl-Shift-Enter
or Cmd-Shift-Return
(or use the Source
button in the toolbar).
2.2 Standard workflow
And now we are finally able to describe the workflow that you will probably use much of the time that you are working with R Studio:
- Create the script file.
- Put the comments at the top with the name of the file.
- Save the file.
- Write the R commands, one by one. While you are constructing each command, use
Ctrl-Enter
with each command as you slowly build it up. This is a much easier way of editing commands than just using theConsole
. It also helps you keep errors and misunderstandings to a minimum as you build the script.
3 More information
You might find this page and this page helpful resources as you learn about R scripts.