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

From: Don MacQueen <>
Date: Tue 24 May 2005 - 07:51:18 EST

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 >')
   system('grep succeeded Runall.out >>')

   cat('See files Runall.out and\n')    cat('Done\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')    TRUE

## 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}}
> mailing list
>PLEASE do read the posting guide!

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

______________________________________________ mailing list
PLEASE do read the posting guide!
Received on Tue May 24 07:54:16 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:32:01 EST