[R] how to stop without error message?

Henrik Bengtsson hb at stat.berkeley.edu
Tue Nov 11 08:50:44 CET 2008


I've also been looking for solutions to similar problems, but I never
found a good solution.  However, I think that if it would be possible
to send an interrupt signal/condition by code (cf. user press Ctrl-C)
one could achieve plenty of things with and without tryCatch().  See
Thread 'New simpleExit() condition (Was: Re: Can example() code stop
the example without generating an error?)' on March 16, 2006:

  http://tolstoy.newcastle.edu.au/R/devel/06/03/4524.html

So, maybe someone else know how to signal an interrupt in a script?

/Henrik

On Mon, Nov 10, 2008 at 6:01 PM, John Fox <jfox at mcmaster.ca> wrote:
> Dear Mark,
>
> I see what you mean. Clearly the problem is with try(), since if you
> eliminate that, the message disappears.
>
> On the other hand, if you use try( f.inner(), silent=TRUE), the message is
> also suppressed.
>
> Does that help?
>
> John
>
>
>> -----Original Message-----
>> From: Mark.Bravington at csiro.au [mailto:Mark.Bravington at csiro.au]
>> Sent: November-10-08 8:22 PM
>> To: jfox at MCMASTER.CA; R-help at r-project.org
>> Subject: RE: [R] how to stop without error message?
>>
>> Hi John
>>
>> Using 'on.exit' to reset the "show.error.messages" option should work, but
> it
>> doesn't quite... and I'm not sure why.
>>
>> It breaks down with, for want of a better phrase, "nested" calls to stop,
>> inside 'try' statements. For some reason, the inner 'stop' calls *do* lead
> to
>> messages, although the outermost one is suppressed.
>>
>> test> f.inner <- function() {
>> +   opt.inner <- options( show.error.messages=FALSE)
>> +   on.exit( options( opt.inner))
>> +   stop( 'leaving f.inner')
>> + }
>> test>
>> test> f.outer <- function() {
>> +   try( f.inner())
>> +   opt.outer <- options( show.error.messages=FALSE)
>> +   on.exit( options( opt.outer))
>> +   stop( 'leaving f.outer')
>> + }
>> test> f.outer()
>> Error in f.inner() : leaving f.inner
>> test>
>>
>> bye
>> Mark
>>
>>
>>
>>
>> --
>> Mark Bravington
>> CSIRO Mathematical & Information Sciences
>> Marine Laboratory
>> Castray Esplanade
>> Hobart 7001
>> TAS
>>
>> ph (+61) 3 6232 5118
>> fax (+61) 3 6232 5012
>> mob (+61) 438 315 623
>>
>> -----Original Message-----
>> From: John Fox [mailto:jfox at mcmaster.ca]
>> Sent: Tuesday, 11 November 2008 10:35 AM
>> To: Bravington, Mark (CMIS, Hobart); R-help at r-project.org
>> Subject: RE: [R] how to stop without error message?
>>
>> Dear Mark,
>>
>> How about something like this?
>>
>> > f <- function(x){
>> +   if (x < 0){
>> +     opt <- options(show.error.messages=FALSE)
>> +     on.exit(options(opt))
>> +     stop()
>> +     }
>> +   sqrt(x)
>> +   }
>> >
>> > f(2)
>> [1] 1.414214
>> > f(-2)
>> >
>>
>> I hope this helps,
>>  John
>>
>> ------------------------------
>> John Fox, Professor
>> Department of Sociology
>> McMaster University
>> Hamilton, Ontario, Canada
>> web: socserv.mcmaster.ca/jfox
>>
>>
>> > -----Original Message-----
>> > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
>> On
>> > Behalf Of Mark.Bravington at csiro.au
>> > Sent: November-10-08 5:42 PM
>> > To: R-help at r-project.org
>> > Cc: Mark.Bravington at csiro.au
>> > Subject: [R] how to stop without error message?
>> >
>> > Dear list
>> >
>> > Can anyone suggest a simple way to abort execution like stop(...) does,
>> but
>> > without issuing an "Error: ..." message?
>> >
>> > I don't want to set 'options( show.error.messages=TRUE)' because I want
>> > normal behaviour to resume after this particular stop.
>> >
>> > (Please reply personally as well as to the list, as I'm not subscribed
> to
>> R-
>> > help)
>> >
>> > Thanks
>> > Mark
>> >
>> > --
>> > Mark Bravington
>> > CSIRO Mathematical & Information Sciences
>> > Marine Laboratory
>> > Castray Esplanade
>> > Hobart 7001
>> > TAS
>> >
>> > ph (+61) 3 6232 5118
>> > fax (+61) 3 6232 5012
>> > mob (+61) 438 315 623
>> >
>> > ______________________________________________
>> > 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.
>>
>
> ______________________________________________
> 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.
>



More information about the R-help mailing list