The hardware and bandwidth for this mirror is donated by dogado GmbH, the Webhosting and Full Service-Cloud Provider. Check out our Wordpress Tutorial.
If you wish to report a bug, or if you are interested in having us mirror your free-software or open-source project, please feel free to contact us at mirror[@]dogado.de.

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:

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

you may either use:

source("/home/jim/psych/adoldrug/partyuse1.R")

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:

C:\JIM\PSYCH\ADOLDRUG\PARTYUSE1.R

I would have to refer to it as:

C:\\JIM\\PSYCH\\ADOLDRUG\\PARTYUSE1.R

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
initial.dir<-getwd()
# change to the new directory
setwd("/home/jim/psych/risk/adol")
# load the necessary libraries
library(nlme)
# set the output file
sink("adolrisk03.out")
# load the dataset
...
# close the output file
sink()
# unload the libraries
detach("package:nlme")
# change back to the original directory
setwd(initial.dir)

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")
brkdn(risk~group,adolrisk03.df)

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.

...
par(ask=TRUE)
xpos<-barplot(loaeval[1,],ylim=c(0,5),main="Lecture evaluation scores",
 names.arg=eval.names)
error.bars(xpos,loaeval)
dev.copy2eps("loaeval.ps",width=6,height=6)
...
par(ask=FALSE)

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

These binaries (installable software) and packages are in development.
They may not be fully stable and should be used with caution. We make no claims about them.
Health stats visible at Monitor.