Hi R experts, I am looking for a simple error handling approach, whereby I could stop function execution with a customized error message. For example: for (i in 1:10) { if (i == 5) # I'd like to be able to stop right here with an error message I have complete control over } The problem with stop() is that I cannot control completely what gets printed to the terminal, even with stop(call.=FALSE) there is the "Error:" string. I've worked through examples (whatever few there are) for try() and tryCatch() and I still cannot understand how to do this. If I supply my own error handler function with tryCatch(..., error=function(e) ...) I can control the error message, but the loop continues on to i=6, etc. So I am struggling with error handling in R... It' seems its a lot simpler and more consistent e.g. in Python. Any help would be greatly appreciated! Aleksey [[alternative HTML version deleted]]
On Nov 16, 2010, at 10:58 AM, Aleksey Naumov wrote:> Hi R experts, > > I am looking for a simple error handling approach, whereby I could > stop > function execution with a customized error message. For example: > > for (i in 1:10) { > if (i == 5){#Then insert: cat("Informative error message") ; break() }> # I'd like to be able to stop right here with an error message > I have > complete control over > } >I think "stop right here" is ambiguous. Are you at a terminal session or inside a script or function? Do you want to call a debugger? And if you were inside a function then just cat("message"); return()> The problem with stop() is that I cannot control completely what gets > printed to the terminal, even with stop(call.=FALSE) there is the > "Error:" > string. I've worked through examples (whatever few there are) for > try() and > tryCatch() and I still cannot understand how to do this. If I supply > my own > error handler function with tryCatch(..., error=function(e) ...) I can > control the error message, but the loop continues on to i=6, etc. > > So I am struggling with error handling in R... It' seems its a lot > simpler > and more consistent e.g. in Python. > > Any help would be greatly appreciated! > > Aleksey > > [[alternative HTML version deleted]]David Winsemius, MD West Hartford, CT
Il giorno mar, 16/11/2010 alle 10.58 -0500, Aleksey Naumov ha scritto:> for (i in 1:10) { > if (i == 5) > # I'd like to be able to stop right here with an error message I > have > complete control over > }I'm far from expert, but perhaps this is near what you are looking for for(i in 1:10){ if (i == 5) {browser()} print("anything")} Browser stop the execution and allows you to indagate about -- Ottorino-Luca Pantani, Universit? di Firenze Dip.to di Scienze delle Produzioni Vegetali, del Suolo e dell'Ambiente Forestale (DiPSA) P.zle Cascine 28 50144 Firenze Italia Ubuntu 10.04 -- GNOME 2.30.2 GNU Emacs 23.1.50.1 (x86_64-pc-linux-gnu, GTK+ Version 2.18.0) ESS version 5.11 -- R 2.12.0
David, Thank you for your help. I agree, "stop right here" is ambiguous, let me clarify. I am at a terminal session, calling my top-level function f(). At any point inside f() I'd like to be able to generate an error message, print it to the terminal and stop execution of f() (but still remain in the R session). No, I do not need to be able to debug the problem, the error message is meant to be sufficient for an end user (I am writing it for my office colleagues, and I don not expect them to do any debugging, merely bring the error message to my attention). break would not be sufficient, since it deals with loops, any my use of for() was just an example illustrating that I'd like to stop function execution at any point.return() is a good suggestion, except that I would need to call it from my main function f(), e.g. f = function(...) { # An error condition 1 cat('an error message') return() # other error conditions # ... } My hope would be to wrap error messaging and execution control (i.e. stopping f()) in one helper function -- to make code more readable and less verbose, e.g.: f = function(...) { # An error condition 1 my_error_function(message) # ... } where my_error_function() simply prints the detailed error message and then stops execution -- i.e. gets out of f(). Does this make sense? Thank you, Aleksey On Tue, Nov 16, 2010 at 11:16 AM, David Winsemius <dwinsemius@comcast.net>wrote:> > On Nov 16, 2010, at 10:58 AM, Aleksey Naumov wrote: > > Hi R experts, >> >> I am looking for a simple error handling approach, whereby I could stop >> function execution with a customized error message. For example: >> >> for (i in 1:10) { >> if (i == 5){ >> > #Then insert: > cat("Informative error message") ; break() } > > > # I'd like to be able to stop right here with an error message I have >> complete control over >> } >> >> > I think "stop right here" is ambiguous. Are you at a terminal session or > inside a script or function? Do you want to call a debugger? > > And if you were inside a function then just cat("message"); return() > > The problem with stop() is that I cannot control completely what gets >> printed to the terminal, even with stop(call.=FALSE) there is the "Error:" >> string. I've worked through examples (whatever few there are) for try() >> and >> tryCatch() and I still cannot understand how to do this. If I supply my >> own >> error handler function with tryCatch(..., error=function(e) ...) I can >> control the error message, but the loop continues on to i=6, etc. >> >> So I am struggling with error handling in R... It' seems its a lot simpler >> and more consistent e.g. in Python. >> >> Any help would be greatly appreciated! >> >> Aleksey >> >> [[alternative HTML version deleted]] >> > > David Winsemius, MD > West Hartford, CT > >[[alternative HTML version deleted]]
On Tue, Nov 16, 2010 at 10:58 AM, Aleksey Naumov <anaumov at gmail.com> wrote:> Hi R experts, > > I am looking for a simple error handling approach, whereby I could stop > function execution with a customized error message. For example: > > for (i in 1:10) { > ? if (i == 5) > ? ? ?# I'd like to be able to stop right here with an error message I have > complete control over > } > > The problem with stop() is that I cannot control completely what gets > printed to the terminal, even with stop(call.=FALSE) there is the "Error:" > string. I've worked through examples (whatever few there are) for try() and > tryCatch() and I still cannot understand how to do this. If I supply my own > error handler function with tryCatch(..., error=function(e) ...) I can > control the error message, but the loop continues on to i=6, etc. > > So I am struggling with error handling in R... It' seems its a lot simpler > and more consistent e.g. in Python. > > Any help would be greatly appreciated! >There was a long thread on this a couple of years ago. You might want to look at this response regarding callCC and also the other responses: http://tolstoy.newcastle.edu.au/R/e5/help/08/11/6967.html -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com