Dear expeRts, is there a neat way to *completely* stop a script after an error occured? For example, consider the following script: ## ==== file.R === for(i in 1:10){ print(i) if(i == 5) stop("i == 5") } for(i in 11:100) print(i) ## =============== stop() behaves like it should namely to stop the execution of the *current* expression, but I was wondering if it is possible to *really* stop the script after the first for loop [so without executing the second for loop or anything after that point]. Of course one could use something like "if(there was an error) do not continue" but that's not really nice. Cheers, Marius
I take you don't use source() to execute your scripts. When using source, stop() aborts the complete script, just as you indent to. -- View this message in context: http://r.789695.n4.nabble.com/How-to-completely-stop-a-script-after-stop-tp3218808p3218823.html Sent from the R help mailing list archive at Nabble.com.
Somehow this reminds me of a famous FORTRAN code snippet: 10 STOP STOP STOP ! IN CASE STILL SKIDDING GOTO 10 <quote> From: Marius Hofert <m_hofert_at_web.de> Date: Sat, 15 Jan 2011 09:09:20 +0100 Dear expeRts, is there a neat way to *completely* stop a script after an error occured? For example, consider the following script: ## ==== file.R === for(i in 1:10){ print(i) if(i == 5) stop("i == 5") } for(i in 11:100) print(i) ## =============== stop() behaves like it should namely to stop the execution of the *current* expression, but I was wondering if it is possible to *really* stop the script after the first for loop [so without executing the second for loop or anything after that point]. Of course one could use something like "if(there was an error) do not continue" but that's not really nice.
Marius - Do you get the behaviour you want if you substitute if(i == 5){cat('i==5\n');quit(save='n')} for the line with the call to stop? - Phil Spector Statistical Computing Facility Department of Statistics UC Berkeley spector at stat.berkeley.edu On Sat, 15 Jan 2011, Marius Hofert wrote:> Dear expeRts, > > is there a neat way to *completely* stop a script after an error occured? > For example, consider the following script: > > ## ==== file.R ===> > for(i in 1:10){ > print(i) > if(i == 5) stop("i == 5") > } > for(i in 11:100) print(i) > > ## ===============> > stop() behaves like it should namely to stop the execution of the *current* > expression, but I was wondering if it is possible to *really* stop the script after > the first for loop [so without executing the second for loop or anything after that > point]. Of course one could use something like "if(there was an error) do not continue" > but that's not really nice. > > Cheers, > > Marius > ______________________________________________ > 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. >
I too am encountering this problem. When I have a large script, if I select all in the editor and then ctrl-r to run, if it encounters a stop() function it simply prints an error message and continues to execute the remainder of the script, as opposed to terminating execution at that line. The quit() function exits R altogether, which I don't want. Yes, I could manually select only the portion of script which I want to run, but for lengthy scripts which I run repeatedly (generally changing only the name of the file I want analyzed), this can be quite tedious. It appears that the only solution is to put most of the code in a separate file and call it using source(); this has the downside of reducing the clarity of the code -- it's a sort-of structural spaghetti code approach. -- View this message in context: http://r.789695.n4.nabble.com/How-to-completely-stop-a-script-after-stop-tp3218808p3436704.html Sent from the R help mailing list archive at Nabble.com.
Seemingly Similar Threads
- Quiz: Who finds the nicest form of X_1^\prime?
- splom, plotmath: how to add three lines of information with alignment?
- How to set an argument such that a function treats it as missing?
- How to create an array of lists of multiple components?
- lattice + plotmath: how to get a variable in bold face?