Skip to main content

1.2) Addressing An Empty Screen


A. Although you can type straight into the GUI window, it’s best to type your commands into a script. Go to the File menu and click on New script; this will open a text editor window which you can arrange alongside the GUI window. Then type your commands into the script window, and send each line to the GUI by putting the cursor in a line and pressing the keys Ctrl and R, together. The script remains as a record of what you’ve done and allows you to repeat commands easily and to save them. Try it now:

# A script
X <- 1:10
Y <- X/2
plot(X, Y, col="darkgreen") # Plot Y against X
points (X, Y^2, col="red") # Add Y-squared

You can also select just part of a script, and then press Crtl and R to dispatch the selected code. Try selecting just the 1:10 in the code above, and running that alone.

Take care when typing! It can be difficult to distinguish the number 1 from a lower-case letter l. But you probably got them right in the exercise we’ve just done – did you?

B. Use a hash (#) to add helpful remarks in your scripts – this is highly advisable and will save a lot of time when you come back to try and understand old script files that you want to re-use. R will ignore everything on a line after a #.