First driving lessons for R-Studio

[revised on 12 December]

Raykov and Marcoulides explain how we can enter single commands in R to calculate statistics and generate graphics. Numerous tutorials also go step-by-step. However the beauty of text-driven commands is that we can write a whole string of them in a separate text-file, then highlight, copy, and paste the command-strings we want into the R console. If something goes wrong, and we see the error, we can edit the text file, then copy-and-paste again to run the commands. When it goes right, save that text file so you can use it again later!

The following are a few basic driving lessons for R-Studio

First: use File > New File > R Script to create a new tab in the upper-left “Source Pane”.

Click File -> New -> R Script; this will create a new blank area.

Click File -> New -> R Script; this will create a new blank area.

Create new commands, or groups of commands, on a “sheet” in the “source Pane”.
You can:
a) type commands directly onto that sheet; or
b) copy them from web pages or text files and paste them onto that sheet; or
c) go to the History tab on the upper-left “Workspace Pane”, find the command you like, and copy it over to the script tab on the upper left. Place the active cursor on the line of the command you like, and click the “To Source” button at the top of the History window.

If you want to run just one line of commands at a time, make sure the cursor is placed somewhere (anywhere) on the first line you want to rune and then press “Run“. R-Studio will automatically paste that line into the lower-left “Console Pane” and run it.

If you want to run a series of commands, one line right after another, make sure all the lines are selected (highlighted in blue), an then press “Run”. R-Studio will paste-and-execute each line, one after another. It will ignore lines that start with a hash-mark (#).

R Script with all command-lines selected.

R Script with all command-lines selected.

Second: you can save script-pages as freestanding .r files. Click the blue floppy-disk icon on that sheet and it will let you name the sheet and save it as a fresstanding .r file in your working directory. I have more on backing up commands and data on a separate page.

Third: whenever you create a successful command, write a comment (or several comments) about the command on the line just above it. Begin the comment with a hash-mark (#) so that R will not mistake the comment as a command.

And Fourth: R saves every command that you execute (within this session) in a History buffer. R-Studio lets you see that sequence of commands, in the “History” tab (upper right “Workspace Pane”). If you typed a command that was almost right, except for some exasperating typo, then press the [UP] arrow key on your keyboard and the previous command will reappear in the command line. Then you can edit the command and re-run it. Once it is successful, you can copy and paste the command onto a worksheet in the upper-left “Source Pane”.