How to *completely* stop a script after stop()?

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

How to *completely* stop a script after stop()?

mhofert
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
______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: How to *completely* stop a script after stop()?

Taras Zakharko
I take you don't use source() to execute your scripts. When using source, stop() aborts the complete script, just as you indent to.
Reply | Threaded
Open this post in threaded view
|

Re: How to *completely* stop a script after stop()?

Phil Spector
In reply to this post by mhofert
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
  [hidden email]


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
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: How to *completely* stop a script after stop()?

algorimancer
In reply to this post by mhofert
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.
Reply | Threaded
Open this post in threaded view
|

Re: How to *completely* stop a script after stop()?

Duncan Murdoch-2
On 08/04/2011 11:47 AM, algorimancer wrote:

> 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.

It sounds as though you are talking about the Windows GUI.  That's
important, because other GUIs probably have different behaviour.

To run a script up to the first error, do this:

Highlight the part you want to run (or Ctrl-a for everything).
Copy the code using Ctrl-c.
In the console, run source("clipboard") (perhaps with echo=TRUE if you
want to see it as it goes).  This is a lot of typing the first time you
do it, but after that, the up arrow can bring back the command.

It would probably make sense for Ctrl-R to do something functionally
equivalent to Ctrl-C, source("clipboard", echo=TRUE) rather than the
current behaviour.  Not going to happen in 2.13.x, but maybe in 2.14.x
in the fall.

Duncan Murdoch

> --
> 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.
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: How to *completely* stop a script after stop()?

William Dunlap
In reply to this post by algorimancer
Put an open brace in the first line of your file
and a close brace in the last line.

I encourage people with scripts long enough that this
is a problem to divide up their work into functions
that a shorter script calls.  (This is akin to UCSD
Pascal on the Osbourne II that refused to deal with
a function that could not fit onto the tiny screen
-- it forced you do divide things up into small
understandable chunks of work.)

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com  

> -----Original Message-----
> From: [hidden email]
> [mailto:[hidden email]] On Behalf Of algorimancer
> Sent: Friday, April 08, 2011 8:47 AM
> To: [hidden email]
> Subject: Re: [R] How to *completely* stop a script after stop()?
>
> 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.
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: How to *completely* stop a script after stop()?

Duncan Murdoch-2
On 08/04/2011 12:40 PM, William Dunlap wrote:
> Put an open brace in the first line of your file
> and a close brace in the last line.

This shares an advantage with the method I suggested:  if there's a
syntax error somewhere in the script, *nothing* will be executed.  It
has the disadvantage of not intermixing input and output the way the
usual cut and paste would do.

Using source("clipboard", echo=TRUE) has a few disadvantages too:

  - lots of typing
  - the automatic colouring of input in red, output in blue is lost
  - run-time error messages are ugly (though syntax errors are presented
nicely).

It's nearly trivial to fix the first of these (make Ctrl-R do this
instead); the 2nd one looks harder.

Duncan Murdoch

> I encourage people with scripts long enough that this
> is a problem to divide up their work into functions
> that a shorter script calls.  (This is akin to UCSD
> Pascal on the Osbourne II that refused to deal with
> a function that could not fit onto the tiny screen
> -- it forced you do divide things up into small
> understandable chunks of work.)
>
> Bill Dunlap
> Spotfire, TIBCO Software
> wdunlap tibco.com
>
> >  -----Original Message-----
> >  From: [hidden email]
> >  [mailto:[hidden email]] On Behalf Of algorimancer
> >  Sent: Friday, April 08, 2011 8:47 AM
> >  To: [hidden email]
> >  Subject: Re: [R] How to *completely* stop a script after stop()?
> >
> >  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.
> >
> >  ______________________________________________
> >  [hidden email] 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.
> >
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: How to *completely* stop a script after stop()?

Jonathan P Daily
In reply to this post by Duncan Murdoch-2
Would options(error = recover) be of some help?
--------------------------------------
Jonathan P. Daily
Technician - USGS Leetown Science Center
11649 Leetown Road
Kearneysville WV, 25430
(304) 724-4480
"Is the room still a room when its empty? Does the room,
 the thing itself have purpose? Or do we, what's the word... imbue it."
     - Jubal Early, Firefly

[hidden email] wrote on 04/08/2011 12:38:37 PM:

> [image removed]
>
> Re: [R] How to *completely* stop a script after stop()?
>
> Duncan Murdoch
>
> to:
>
> algorimancer
>
> 04/08/2011 12:40 PM
>
> Sent by:
>
> [hidden email]
>
> Cc:
>
> r-help
>
> On 08/04/2011 11:47 AM, algorimancer wrote:
> > 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.
>
> It sounds as though you are talking about the Windows GUI.  That's
> important, because other GUIs probably have different behaviour.
>
> To run a script up to the first error, do this:
>
> Highlight the part you want to run (or Ctrl-a for everything).
> Copy the code using Ctrl-c.
> In the console, run source("clipboard") (perhaps with echo=TRUE if you
> want to see it as it goes).  This is a lot of typing the first time you
> do it, but after that, the up arrow can bring back the command.
>
> It would probably make sense for Ctrl-R to do something functionally
> equivalent to Ctrl-C, source("clipboard", echo=TRUE) rather than the
> current behaviour.  Not going to happen in 2.13.x, but maybe in 2.14.x
> in the fall.
>
> Duncan Murdoch
>
> > --
> > 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.
> >
> > ______________________________________________
> > [hidden email] 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.
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: How to *completely* stop a script after stop()?

Duncan Murdoch-2
On 08/04/2011 1:29 PM, Jonathan P Daily wrote:
> Would options(error = recover) be of some help?

No, that will probably be very confusing.  The problem is that the
Windows GUI uses Ctrl-R as a short form of "cut from the editor, paste
to the console", and it will paste the whole text regardless of whether
it works or not.  If recover was triggered, it would be sent the
remainder of the script, which would probably lead to a long sequence of
errors (recover is looking for an integer).

Duncan Murdoch

> --------------------------------------
> Jonathan P. Daily
> Technician - USGS Leetown Science Center
> 11649 Leetown Road
> Kearneysville WV, 25430
> (304) 724-4480
> "Is the room still a room when its empty? Does the room,
>   the thing itself have purpose? Or do we, what's the word... imbue it."
>       - Jubal Early, Firefly
>
> [hidden email] wrote on 04/08/2011 12:38:37 PM:
>
> >  [image removed]
> >
> >  Re: [R] How to *completely* stop a script after stop()?
> >
> >  Duncan Murdoch
> >
> >  to:
> >
> >  algorimancer
> >
> >  04/08/2011 12:40 PM
> >
> >  Sent by:
> >
> >  [hidden email]
> >
> >  Cc:
> >
> >  r-help
> >
> >  On 08/04/2011 11:47 AM, algorimancer wrote:
> >  >  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.
> >
> >  It sounds as though you are talking about the Windows GUI.  That's
> >  important, because other GUIs probably have different behaviour.
> >
> >  To run a script up to the first error, do this:
> >
> >  Highlight the part you want to run (or Ctrl-a for everything).
> >  Copy the code using Ctrl-c.
> >  In the console, run source("clipboard") (perhaps with echo=TRUE if you
> >  want to see it as it goes).  This is a lot of typing the first time you
> >  do it, but after that, the up arrow can bring back the command.
> >
> >  It would probably make sense for Ctrl-R to do something functionally
> >  equivalent to Ctrl-C, source("clipboard", echo=TRUE) rather than the
> >  current behaviour.  Not going to happen in 2.13.x, but maybe in 2.14.x
> >  in the fall.
> >
> >  Duncan Murdoch
> >
> >  >  --
> >  >  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.
> >  >
> >  >  ______________________________________________
> >  >  [hidden email] 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.
> >
> >  ______________________________________________
> >  [hidden email] 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.
>

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: How to *completely* stop a script after stop()?

algorimancer
In reply to this post by Duncan Murdoch-2
Thank you all for the astoundingly quick responses.  I think that the bounding open/closed braces approach sounds like the easiest solution for the moment -- though I look forward to seeing this all automated in a future version of R :)

Incidentally, I have indeed encapsulated much of the code as functions which I call using source(); the problematic (lengthy) front-end is a decision tree which tailors a specific analysis to designated files.  This mimics the switch/case structure which I'm more familiar with in c/c++.  There's a limit to how much can be neatly moved to a separate file.

Thank you again for your assistance.