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.
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))
$ Revo CMD BATCH myscript.R myscript.Rout
$
Revo < myscript.R > myscript.Rout
- 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).
$ Revo CMD BATCH --slave myscript.R 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
Thanks for this! With these instructions I was finally able to get started with R in command line mode!
Steve
Posted by: Steve | December 17, 2009 at 17:15
That's great, really appreciate your help. save me lot of time to investigate how to run R in Java, many thanks.:)
Posted by: Lu Li | December 21, 2009 at 21:43
Some comments about your list of differences between Revo and R:
* in UNIX world: stdin is 0, stdout is 1, stderr is 2; so, if you are already redirecting command output to a file and would like to append the errors into it, add 2>&1 to your command. Ex: mycmd >outfile 2>&1
BUT, because R CMD BATCH is already appending stderr to stdout in its output, it is not necessary here.
* argument --no-timing will prevent the call to proc.time()
Posted by: Benoit Borrel | February 09, 2010 at 08:52