Last week, I posted a link to a primer on running R in batch mode, by redirecting the input and output of the R shell command. (By the way, it works the same way in REvolution R too: just replace the command R with Revo). In the comments, Doug Bates reminded me that there's a better way: using R CMD BATCH.
Let's illustrate by example. Suppose I have a script file containing the commands for a statistical analysis. I want to run it in batch mode (perhaps because it will run overnight), and save the output of the commands to a file. Here's my script file, saved as myscript.R:
clotting <- data.frame(
u = c(5,10,15,20,30,40,60,80,100),
lot1 = c(118,58,42,35,27,25,21,19,18),
lot2 = c(69,35,26,21,18,16,13,12,12))
cat("Model data:\n")
print(clotting)
warning("Model starting")
obj <- glm(lot1 ~ log(u), data=clotting, family=Gamma)
cat("\nEstimated parameters:\n")
coef(summary(obj))
I can run it in batch mode from the shell or Windows command line, like this:
$ Revo CMD BATCH myscript.R myscript.Rout
$
(Note: I'm using REvolution R here, so replace Revo with R if you're using CRAN R. $ is the shell prompt, so don't type that.) This has a similar effect to the file redirection method:
Revo < myscript.R > myscript.Rout
with a few differences:
- CMD BATCH automatically captures warnings and errors, which the file redirection method as shown above would lose: they'd be displayed at the shell prompt, not captured in the file. (It is achievable with some tricky shell syntax, but I can never remember how to do it.)
- CMD BATCH automatically adds a call to proc.time() at the end of the session, to document how long the script took to run.
- The CMD BATCH syntax is easier to remember (at least for me).
By default, both methods show both the prompts and input commands and their outputs in the output file
myscript.Rout, just as if you'd typed them at the interactive prompt. But if you only want the outputs (you're building a text report using R commands, say), a very handy option is
--slave: this suppresses the prompts, commands, and R's startup blurb to leave you only with the output. With the command:
$ Revo CMD BATCH --slave myscript.R myreport.txt
I get the following in the file myreport.txt:
Model data:
u lot1 lot2
1 5 118 69
2 10 58 35
3 15 42 26
4 20 35 21
5 30 27 18
6 40 25 16
7 60 21 13
8 80 19 12
9 100 18 12
Warning message:
Model starting
Estimated parameters:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -0.01655438 0.0009275466 -17.84749 4.279149e-07
log(u) 0.01534311 0.0004149596 36.97496 2.751191e-09
> proc.time()
user system elapsed
1.153 0.061 1.917
One last point: you can actually pass your own command-line arguments into R to affect the behaviour of your script. Maybe we'll look at that in detail in some other post, but for now the help for the function
commandArgs will get you started. You might also want to use
Rscript instead, which we've
discussed before.