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

Uwe Ligges ligges at statistik.uni-dortmund.de
Wed May 25 10:30:51 CEST 2005


Don MacQueen wrote:

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

I don't see a reason why your functions are writing character strings 
using cat() into files, and after that processing those files using 
system calls and tools (e.g. grep) which are not available on all 
platforms.

 From my point of view the R way is to save the state into R objects 
(e.g. a vector of status codes with one element for each "R script").
Then you can easily use R functions to claculate on those objects.

Uwe Ligges



> 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 at 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
> 
> 
>




More information about the R-help mailing list