enocko
2012-Aug-11 08:07 UTC
[R] Stopping all code execution when ANY error occurs (OR error handling without try/tryCatch)
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.
Bert Gunter
2012-Aug-11 14:19 UTC
[R] Stopping all code execution when ANY error occurs (OR error handling without try/tryCatch)
1. You probably need to tell us on what platform (OS, version) you are running, as requested by the posting guide. 2. To me, it sounds like your question is: How can I avoid using the error trapping tools built into R but still trap errors. If so, pretty nonsensical, no? 3. But maybe someone else can suggest something useful. My thought is that what you really need is to think out of the box and use a better IDE. Check out CRAN for alternatives, but ESS, RStudio, Tinn-R, and WInedit immediately come to mind. what will work for you depends on your platform (ergo 1 above). Cheers, Bert On Sat, Aug 11, 2012 at 1:07 AM, enocko <enotrash at gmail.com> 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.-- Bert Gunter Genentech Nonclinical Biostatistics Internal Contact Info: Phone: 467-7374 Website: http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm
Uwe Ligges
2012-Aug-11 14:20 UTC
[R] Stopping all code execution when ANY error occurs (OR error handling without try/tryCatch)
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. >