Kickstarting R - Writing R scripts

So what is an R script?

An R script is simply a text file containing (almost) the same commands that you would enter on the command line of R. (almost) refers to the fact that if you are using sink() to send the output to a file, you will have to enclose some commands in print() to get the same output as on the command line. To run a script, let's say one with the name:


you may either use:


on the command line of R OR

R CMD BATCH /home/jim/psych/adoldrug/partyuse1.R

Let's pause to note a little historical bifurcation that has caused almost as much strife as the question of exactly what the <Delete> key should do. The convention that the slash (/) character is used as a separator in the notation for a filesystem path is not universal. PC-DOS, for example, used the backslash (\). Like Fords vs Chevrolets, French vs English, and whether you crack the egg at the big or small end, it doesn't really matter a rat's behind which one you use as long as it works. To make it work in R, however, where the *NIX convention of using the backslash as an escape character is respected, you will have to double backslashes in paths for them to be read properly. Thus if the filename above was:


I would have to refer to it as:


in an R script.

Using the script to determine where things happen

Managing the various objects used in R can be challenging. As mentioned in Starting Up, using the directory structure to sort these objects into sensible categories can be a big help. Instead of starting the R session in a particular directory, you may wish to keep a directory of R scripts and allow these to change the working directory to suit whatever task they perform.

First, choose your directory. This will be the place where all of your R scripts will be stored. You can either change to this directory before starting R or use the method described in Starting Up. Let's examine the beginning and end of a typical script written for this purpose:

# store the current directory
# change to the new directory
# load the necessary libraries
# set the output file
# load the dataset
# close the output file
# unload the libraries
# change back to the original directory

First notice that the script stores the initial directory and changes to the directory where the data is, and where output will be generated. The libraries necessary for the analysis are then loaded. Finally, the output of the script is directed to a text file, producing a record of the analysis. Then comes the action. After the analysis has been completed, the output file is closed, the libraries are unloaded, and the directory is changed back to where the scripts reside. At this point, you may want to start another script or shut down R.

Annotating the output file

R's output can often be terse. Annotating it will make it much easier for someone unfamiliar with R or statistical analysis to understand.

cat("\nMean risk-taking scores for all groups\n\n")

Here cat() is used to label a breakdown of means, leaving a blank line above and two below the label. Spacing the output with meaningful labels improves the appearance and comprehensibility.

Viewing and saving plots

One potential disadvantage of scripts is that multiple plots will flash by in a blur if you let them.

xpos<-barplot(loaeval[1,],ylim=c(0,5),main="Lecture evaluation scores",

par(ask=TRUE) requires you to hit <Return> before each plot is displayed. If you want such a prompt after display you can use:

readline("Press <Enter> to continue")

Remember that the little message will show up in the command window and the user will have to switch to that window to restart the action by pressing <Enter>. If you want a little window to pop up with a flashing message that will alert the near-unconscious, get into the Tcl-Tk interface. It's amazing. The example then uses dev.copy2eps() to copy the display to a Postscript file so that the plot may be printed or inserted into a document.

For more information, see An Introduction to R: Device Drivers.

Back to Table of Contents