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

From: Don MacQueen <macq_at_llnl.gov>
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('========================================================\n')
cat('Running script "Runall.r" at',format(t0runall),'\n')
cat('========================================================\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:

cat('========================================================\n')
t1 <- Sys.time()
cat('[Runall.r] Elapsed
time',format(t1-t0runall),attributes(t1-t0runall)$units,'\n')

if (hc) {

   cat('Done\n')
   sink()

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

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

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

  rm.trymsg <- function(msg) {
   if (class(msg)=='try-error') {

     cat('============',tblid,'failed =============\n')
     return(FALSE)

   }
   if (data.class(msg)=='list'& unlist(msg)[[1]]=='bad.table') {
     cat('============',tblid,'failed ===== bad.table ==========\n')
     return(FALSE)

   }
   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:
>Hello,
>
>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:
>
>Ex:
>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 1.1.1.1 -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.
>
>Thanks
>
>Pierre Lapointe
>
>
>
>***********************************************************************************
>AVIS DE NON-RESPONSABILITE:\ Ce document transmis par courri...{{dropped}}
>
>______________________________________________
>R-help@stat.math.ethz.ch mailing list
>https://stat.ethz.ch/mailman/listinfo/r-help
>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

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
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