[R] Calling R from R and specifying "wait until script is finished"

Don MacQueen macq at llnl.gov
Mon May 23 23:51:18 CEST 2005

I don't know about efficient, but here is a way that I find to be 
practical, with around 100 R scripts.

I create a master R script (I call it "Runall.r"). It begins like this:

## Execute me with  R --save < Runall.r >& Runall.log

hc <- TRUE
if (hc) sink('Runall.out')

t0runall <- Sys.time()
cat('Running script "Runall.r" at',format(t0runall),'\n')

msg <- try(source('ae-175.r')) ; rm.trymsg(msg)
msg <- try(source('ae-235.r')) ; rm.trymsg(msg)
msg <- try(source('ae-251.r')) ; rm.trymsg(msg)
msg <- try(source('ae-331.r')) ; rm.trymsg(msg)
msg <- try(source('ae-332.r')) ; rm.trymsg(msg)
msg <- try(source('ae-491.r')) ; rm.trymsg(msg)
msg <- try(source('ae-695.r')) ; rm.trymsg(msg)
msg <- try(source('ae-801.r')) ; rm.trymsg(msg)

## and so on, for as many scripts as I want to run
## although I constructed the list of scripts to run by hand, it 
could easily be done
## as a loop, with the script names constructed from the loop index

## the script ends with:

t1 <- Sys.time()
cat('[Runall.r] Elapsed 

if (hc) {

   system('grep failed Runall.out > Runall.info')
   system('grep succeeded Runall.out >> Runall.info')

   cat('See files Runall.out and Runall.info\n')

#### The function rm.trymsg() is this:

  rm.trymsg <- function(msg) {
   if (class(msg)=='try-error') {
     cat('============',tblid,'failed =============\n')
   if (data.class(msg)=='list'& unlist(msg)[[1]]=='bad.table') {
     cat('============',tblid,'failed ===== bad.table ==========\n')
   cat('=========',tblid,'succeeded =========\n')

## and the purpose of using try() and rm.trymsg() is to let the job 
continue if an error occurs in one of
## the scripts. Note, however, that the text strings "bad.table" and 
"tblid" are unique to the task I am doing, and would not
## work in general.

At 8:51 PM -0400 5/21/05, Lapointe, Pierre wrote:
>Let's say I have 50 R scripts to run.  What would be the most efficient way
>to run them?
>I thought I could do multiple Rterms in a DOS batch file:
>Rterm <1.R> 1.txt
>Rterm <2.R> 2.txt
>Rterm <50.R> 50.txt
>However, I'm afraid they will all open at the same time.   I know I could
>pause the batch file with something like:
>PING -n 1 -w 60000 >NUL  (to delay 60 seconds)
>But that would require that I know how long each of my scripts take.
>Is there an easier way?  Something like calling R from R and specifying that
>the script has to be finished before continuing.
>Pierre Lapointe
>AVIS DE NON-RESPONSABILITE:\ Ce document transmis par courri...{{dropped}}
>R-help at stat.math.ethz.ch mailing list
>PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html

Don MacQueen
Environmental Protection Department
Lawrence Livermore National Laboratory
Livermore, CA, USA

More information about the R-help mailing list