Running R in the Background

Last updated Tue Feb 15 10:41:35 EST 2005

Basically you want to run something like

  R CMD BATCH infile outfile &

This may give you more output in outfile than you really want.  If you
want finer control over what is saved, experiment a bit with the
following to figure out how to get what you want.

To stop all the usual R command line stuff from being written to the
outfile, make

options(echo=FALSE)

the first line in infile.

Another thing that is useful is at the end of your file you can have a
line like

save(obj1, obj2, obj3, file = "sim1.Rdata")

This will save obj1, obj2, and obj3 (they might be some objects
containing simulation results) in the file sim1.Rdata.  If you do this
and run the simulation with a command like

R CMD BATCH --no-save infile outfile &

then nothing will be saved in the .Rdata file.  Actually, I usually
use something like

R CMD BATCH --no-save --no-restore infile outfile &

This runs the commands in infile (I would call it infile.R or
infile.Rbatch) with a "clean" R, i.e., it does not read the .Rdata
file in the current directory.  However, if you do it this way you
have to remember to source the functions that you need.