[R] Stopping all code execution when ANY error occurs (OR error handling without try/tryCatch)

Uwe Ligges ligges at statistik.tu-dortmund.de
Sat Aug 11 16:20:50 CEST 2012


Easy: Wrap your code into a function. A function by default exits on the 
first error.

Best,
Uwe Ligges





On 11.08.2012 10:07, enocko wrote:
> Hi all,
>
> I often run a bunch of code with Run Selection, and, after it's done
> running, I find there have been like 20 errors, all due to an error that
> occurred early in my code, which caused problems from there on. (I must then
> scroll up through lots of code to find it.)
>
> What I would like is for R to stop executing when an error occurs.
>
> I am aware of a few solutions:
> 1. Put all the code in a function
> 2. Put all the code in a tryCatch statement with error handling
> 3. *partial solution only* Replace stop() with my own function, so at least
> if the errors are coming from a call to stop, I can catch them and halt all
> code execution. (I do this halting with a hack involving quit() and .Last(),
> so also please let me know if there's a better way)
>
> However, I don't want to put all the code in braces while I'm just
> developing/playing around with code. I run various snippets of code all the
> time, and it's time consuming to always make a tryCatch or function
> surrounding whatever I'm executing.
>
> Is there any way to do a kind of global error handling, so I could source()
> some file once and then have it set for the rest of my session that day?
>
> To see multiple errors that R doesn't stop, you can put these 2 lines into a
> script and do Run Selection:
> 5[[2]] <- 1
> list() <- 1
>
> You'll notice that 2 errors occur, whereas what I want is for execution to
> just stop after the first error, so that the second line never gets run.
>
> Thanks very much,
>
> Phil
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Stopping-all-code-execution-when-ANY-error-occurs-OR-error-handling-without-try-tryCatch-tp4640023.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list