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
Does anyone know how to get Revo to pick the 64 bit version of the engine in command mode. When I use Revo CMd BATCH, I see that the Rterm*32 is running.
Posted by: Tanzy Love | October 20, 2011 at 08:13
I think you will need to specify the path to C:\Revolution\R-Enterprise-4.3\R-2.12.2\bin\x64 on your command line, e.g:
C:\Revolution\R-Enterprise-4.3\R-2.12.2\bin\x64\Rcmd BATCH C:\mybatchtest.R C:\mybatchtest.out
or:
C:\\Revolution\R-Enterprise-4.3\R-2.12.2\bin\x64\Rterm.exe --no-restore --no-save < C:\mybatchtest.R > C:\lmbatchtest.out 2>&1
Posted by: Stephen Weller | October 21, 2011 at 16:37
Thanks, this did it for me!!
C:\\Revolution\R-Enterprise-4.3\R-2.12.2\bin\x64\Rterm.exe --no-restore --no-save --args "nsim.matrix=37; Nsim=25; which.est=1; which.sim=1" < sim33c.R > sim37.Rout 2>&1
Posted by: Tanzy Love | October 27, 2011 at 12:42
This is great. Have you ever looked at creating image output? For example, could we do something like this?
Revo < createPlot.R > myplot.png
Posted by: John | February 16, 2012 at 13:57
Hi John,
If your goal is to be able to specify an output file name from the command line (to customize behavior of a script), one way is like this:
Revo createplot.R --args myplot.png
and in the createplot.R script use commandArgs(trailingOnly=FALSE) to access the specified output file name (here myplot.png).
You might also want to take a look at littler.
Posted by: David Smith | February 16, 2012 at 14:29
I have a question, is there anyway i can make php run a R commands file on windows ? it is essential that whenever i run a php script in starts R and processes this R text file..
Thank you very much!
Posted by: Mohamed | November 02, 2012 at 13:23
How do you do this if you are working with a remote server? How you "send" your code to the R on the server? Do you run your code on the server, then save it there?
Posted by: walkingon2001 | February 21, 2013 at 17:20
I figured it out. Thanks!
Posted by: walkingon2001 | February 21, 2013 at 19:20
Dear sir,
I am using PennCNV package for CNV analysis.
Here I am using visualize_cnv .pl for JPG output file. I have installed R in my system, and in Environmental variable I have putted the path C:\Rtools\gcc-4.6.3\bin;C:\Program Files\R\R-2.15.2\bin\x64; and I am running the command
visualize_cnv.pl -format plot -signal offspring.txt ex1.rawcnv
and I am getting the error:cannot execute system command R CMD BATCH.
How can I resolve the error.
Posted by: Nitin | March 15, 2013 at 11:15
Is it possible to customize the prompt (data input) from the actual output in the console?
We would like to have a visual differentiation between what we input and the results (output).
Posted by: Eduardo Valdes | July 26, 2013 at 13:58