4

I know I must be making a simple syntax mistake, but I want to have a windows batch file that fires up 9 instances of R and runs a different routine in each one. I want these to run simultaneously (i.e. asynchronously). I can fire up 9 command prompt windows and type a command in each one, but it seems like with the START command I should be able to make them start from a single batch file.

Here's an example of how I start one of the instances of R:

"C:\Program Files (x86)\R\R-2.8.1\bin\R" CMD BATCH "C:\Users\jd\Documents\mexico\Estado\getdata1.r" 

Reading this previous stackoverflow question along with this previous question makes me think I should be able to do this:

START "" "C:\Program Files (x86)\R\R-2.8.1\bin\R" CMD BATCH "C:\Users\jd\Documents\mexico\Estado\getdata1.r" /b

That does not return an error, it just returns a prompt and R never starts. What am I missing?

Community
  • 1
  • 1
JD Long
  • 59,675
  • 58
  • 202
  • 294
  • Is "/b" a parameter for the START command? I think it gets passed to the R program. Try changing it to START /b .... but I'm not sure that this really explains the problem (R never starts). – ars Jul 27 '09 at 17:01
  • @Roman, I ended up building Segue :) If I were to try and solve that same problem today I would use Multicore or doRedis. – JD Long Jul 18 '11 at 14:27

3 Answers3

5

I would do two things differently:

  1. Use R itself to dispatch nine different jobs; the snow package is very good at this even when do not use MPI / PVM / NWS for distributed work. Some examples for snow use are for example in my 'introduction to high performance computing with R' tutorials linked from this page. With snow, you get 'parallel' versions of the apply functions that you can run over multiple instances of R running on the local computer (or of course a network of computers if have one). The r-sig-hpc list is helpful for more detailed questions.

  2. Switch to using Rscript.exe instead of using 'R CMD BATCH'. On Linux / OS X you also get a choice of using littler

That said, I run almost all my jobs on Linux so there may be a Windows-specific answer here too that I just do not know. But the above is generic and stays in the platform-agnostic spirit of R.

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
  • OK that makes really good sense. I need to break down and spend a day wrapping my feeble brain around snow and MPI. Parallel process is certainly the future, but I've been slow to get ramped up. – JD Long Jul 27 '09 at 15:00
  • 1
    And the nice thing is that you do can experiment with snow using just network sockets on the local machine before jumping to MPI. That said, nine jobs on a single machine may exhaust your memory anyway. – Dirk Eddelbuettel Jul 27 '09 at 15:11
4

Simple answer. On windows when running command use "Rcmd" not "R CMD". There is a separate exe for running the commands. Look in the bin folder of your R installation.

Andrew Redd
  • 4,632
  • 8
  • 40
  • 64
2

It was not immediately clear from the other answers how to actually make this work (without resorting to parallel processing alternatives, so here is a solution I found that works very simply on windows

If you have a simple r file:

for(i in 1:10){
  ptm0 <- proc.time()
  Sys.sleep(0.5)  
  ptm1=proc.time() - ptm0
  jnk=as.numeric(ptm1[3])
  cat('\n','It took ', jnk, "seconds to do iteration", i)
}

On CMD, specify the directory of where your script is and then start a new window with Rscript to run your code. Multiple lines will open multiple r instances that run your code that also reproduce the messages that the code outputs.

cd "C:\rcode"
START "" Rscript example_code.r /b
START "" Rscript example_code.r /b

If Rscript is not on the system path, just specify the full path instead:

START "" "C:\Program Files\R\bin\x64\Rscript.exe" text_within_loop.r /b
Lucas Fortini
  • 2,420
  • 15
  • 26