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 <dwinsem...@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]]

______________________________________________
R-help@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.

Reply via email to