Much of the time, R is used interactively: a user (like you or me) sits in front of a computer, and types instructions in the R language at the command line. The instructions are executed, the result is displayed on-screen, and then R waits patiently for the next command.
But did you know that R can also be used non-interactively? You can prepare a sequence of commands in advance as a script file and have R execute those commands in batch mode, without ever waiting for human intervention.
Batch mode is useful in many situations. Perhaps you have a complex simulation or analysis that will run for several hours: create a script and run it in batch mode to keep the R console from cluttering your screen (but be sure to save the output in a file to check for errors). If you want to run the job overnight when your computer isn't being used, you can schedule the batch job to start at midnight using your operating system's scheduling tools. Or it might be a production application: a web-server might kick off an R batch job on demand when a customer submits a request on a web form. By the way, even though batch jobs are non-interactive that isn't to say they have no inputs: you can still use all of R's tools to input data from files, web sites -- even running machinery.
If you haven't used batch mode before, it's pretty easy: all you need is a script file and access to your operating system's Command window (Commands on Windows, Terminal on Macs, or a shell window on Linux/Unix). Andrej Kastrin has a nice primer on R's batch mode to get you started.
Andrej Kastrin's Blog: Batch processing with R
Most people run it from the command line?
I thought running R from scripts was the standard method. I can't imagine doing it any other way, even for simple things. It's just so much easier to type the command in a text editor, highlight it, and hit CTRL-R to run it.
I used to work in a computer lab, and one night I used 15 computers from the extra lab to run 15 different parts of my analysis simultaneously. The whirring hum of 15 processor fans hard at work for you is soothing. =)
Posted by: Jesse | June 19, 2009 at 10:44
I second that. I wouldn't even start a simple exploratory analysis directly from the interactive prompt -- how else are you going to keep track of what you're doing? And if you actually use R for work, and not just research, you'll be writing functions and calling them (and R itself) and generally scripting away as much as possible.
Still, good to link -- if anyone really does mostly run R interactively, they should see there is another way.
Posted by: wcw | June 19, 2009 at 17:34
I use R that way too -- cut-n-paste from a script file is much faster that typing. That's still using R interactively though -- you're just automating the typing part :)
Posted by: David Smith | June 19, 2009 at 19:38
I'm surprised that in a discussion of batch mode in R there is no mention of R CMD BATCH. Certainly on Mac OS (which the author is using) and on Linux that is the preferred method of doing batch runs. Instead of
R --vanilla --slave < hello_world.R > result.txt
one would use
R CMD BATCH hello_world.R
Posted by: Douglas Bates | June 20, 2009 at 05:51
I agree with Douglas Bates observation, ages ago I posted a basic tip about running R scripts in batch mode:
http://onertipaday.blogspot.com/2007/05/running-r-programs-on-clusters.html
HIH
Posted by: Paolo | June 21, 2009 at 02:21
I would also recommend Rscript (v easy to run and also handle command line arguments). I've posted this suggestion back in more detail to the original blog too.
Posted by: Edward Ratzer | June 21, 2009 at 14:30
Good point Doug -- R CMD BATCH was a big omission! Thanks -- I feel a new post coming on...
Posted by: David Smith | June 22, 2009 at 10:01
Very informative article and thanks for sharing it. I think most easiest thing is that people can cut/paste R from file script rather than typing and this would easily run R and saves time.
For used desktops log on to http://www.electrocomputerwarehouse.com
Posted by: cheap computer | June 01, 2010 at 00:29