0

I am running a batch file (.bat) to run an R script which works fine. My batch file is ran daily with my task manager. What I am trying to do now is figure out how to pause my batch file if the condition in R script stops the R script.

My batch file:

@echo off
"C:\Program Files\R\R-3.5.2\bin\Rscript.exe" C:\Auto_R_Reports\Codes\SICK_WTGs.R

My R script:

if(NROW(Sick_WTG_USA) == 0){
    stop('NO SICK WTGs')
} else {source("./Codes/2WKcheck.R")}

The idea is that when the if condition in R script evaluates true and my R script stops, I also want my batch file to pause so that I know it stopped and paused.

Mofi
  • 46,139
  • 17
  • 80
  • 143
BB.squared
  • 113
  • 13
  • Pause until when? You want to press a button for example and it continues? – LyzandeR Jul 08 '19 at 14:30
  • 2
    See [Make R exit with non-zero status code](https://stackoverflow.com/questions/7681199/) and let your R script quit with a non-zero exit code like `1` and append in batch file at end of `rscript.exe` command line `|| pause` or add as next line `if errorlevel 1 pause`. See also [Single line with multiple commands using Windows batch file](https://stackoverflow.com/a/25344009/3074564). – Mofi Jul 08 '19 at 14:47
  • 2
    Or if your R script outputs a specific error string to standard output you could also append on `rscript.exe` command line `| %SystemRoot%\System32\find.exe "error string" >nul && pause`. But the first option with non-zero exit code on an error is definitely the better solution. – Mofi Jul 08 '19 at 14:50
  • Thanks @Mofi, that work for me, both answers. – BB.squared Jul 08 '19 at 15:27

0 Answers0