[R] Simple error handling in R

David Winsemius dwinsemius at comcast.net
Tue Nov 16 17:16:23 CET 2010


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



More information about the R-help mailing list