0

I have a program flow where a database command button writes a file with data from the current record, then executes a batch file (windows) to knit a markdown file using the output from the database as inputs.

"C:\Program Files\R\R-4.1.1\bin\i386\Rscript.exe" -e "library('knitr'); rmarkdown::render('MyMarkdownFile.Rmd', output_file='MyOutput.html')"

The final step is that this file is opened in a browser.

start "" "MyOutput.html"

I do not use a unique file name, the same html file (MyOutput.html in the example above) is over-written each time. Sometimes the markdown process throws an error and halts execution during the knit. In these cases the previous version of the html file is then opened by the next batch command and, to the users, this may be confusing: they may assume they are seeing the current report when in fact they are not. (Note there are clear labels to distinguish, but still ...). I am wondering if there is a way to somehow "know" within the batch file that there has been an error in the knit process and thereby halt execution of the batch so that the html is not opened in the final step.

bmacwilliams
  • 165
  • 6
  • 2
    Please open a [command prompt](https://www.howtogeek.com/235101/), run `if /?` and read the output help, especially the part about `IF [NOT] ERRORLEVEL number command`. Then read [single line with multiple commands using Windows batch file](https://stackoverflow.com/a/25344009/3074564) I assume that `Rscript.exe` exits with a value greater 0 on an error, otherwise a real R script should be used which handles the error information returned by the used library function. – Mofi Apr 14 '22 at 16:56

1 Answers1

0

See Mofi's comment. This is the answer. Simply inserting && between batch commands halts execution when the knit returns an error. Thank you.

bmacwilliams
  • 165
  • 6