45

I wrote a script that should stop execution if the supplied data is incorrect. However, although stop produces an error message, the script just continues. A minimal example:

if (TRUE) {stop("End of script?")} #It should stop here
print("Script did NOT end!") # but it doesn't, because this line is printed!

Console output:

> if (TRUE) {stop("End of script?")}
Error: End of script?
> print("Script did NOT end!")
[1] "Script did NOT end!"
>

This is actually not surprising, because from ?stop:

stops execution of the current expression and executes an error action.

So it only ends the current expression, not the script. I have found here that you can wrap {} around the total script (or put it in a function), but that seems rather a workaround than a solution. Off course, it is good programming practice to catch error and handle them off yourself (see for example the link in comment from mra68), but I would still like to know if I can stop a script in R.

I have also tried return and break, but this only works in a function or loop. I searched for other possible keywords like "halt" and "end", but no luck. I am feeling a bit stupid, because it seems a very basic question.

So, is there a command that can make my script halt/stop/end with a fatal error ?

I am running R 3.2.3 on Windows 8, but had the same problem with R 3.0.1 at MAC-OSX.

> sessionInfo()
R version 3.2.3 (2015-12-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)

locale:
[1] LC_COLLATE=Dutch_Netherlands.1252  LC_CTYPE=Dutch_Netherlands.1252    LC_MONETARY=Dutch_Netherlands.1252
[4] LC_NUMERIC=C                       LC_TIME=Dutch_Netherlands.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
[1] tools_3.2.3

TEST ON MAC-OS, sessionInfo()

R version 3.0.1 (2013-05-16)
Platform: x86_64-apple-darwin10.8.0 (64-bit)

locale:
[1] nl_NL.UTF-8/nl_NL.UTF-8/nl_NL.UTF-8/C/nl_NL.UTF-8/nl_NL.UTF-8
Community
  • 1
  • 1
RHA
  • 3,677
  • 4
  • 25
  • 48
  • 2
    I suggest, you put whatever code you want to run in a function then call that function. Then you can use `stop` and it should terminate the function at that point. The function statement in r can also be used to declare methods like in other programming languages, which can be misleading, I guess. – vanao veneri Jan 14 '16 at 10:35
  • 1
    On my system (R version 3.2.3, operating system Windows 7) "stop" stopped the script. If you look at the source code of "stop" you see that its behavior depends on some internal functions, and these might be machine dependent. – mra68 Jan 14 '16 at 11:44
  • [This link](http://adv-r.had.co.nz/beyond-exception-handling.html) might be useful. But if it is, the question is not at all basic. – mra68 Jan 14 '16 at 12:24
  • @mra68 That is odd, as I am running R 3.2.3 on Windows 8. And I've tried it on mac OS-X yesterday. You sure the print commands isn't run on your system? Thanks for the link, it is rather a long read, but as Hadley wrote it, it will probably be worth reading... – RHA Jan 14 '16 at 13:01
  • @RHA: Yes I'm sure. What happens if you try `q()`? On my system it breaks down the whole R session. Perhaps put your `sessionInfo()` into the question. – mra68 Jan 14 '16 at 13:28
  • @RHA: I can also use `if (TRUE) {.Internal(.dfltStop("End of script?", TRUE))}` to stop the script. Perhaps you have a different internal function `.dfltStop`. – mra68 Jan 14 '16 at 13:49
  • @RHA: I tried `if (TRUE) {if (NA) 0}`. It also stops the script. – mra68 Jan 14 '16 at 14:03
  • @mra68 q() not only stops the script but also quits Rstudio. That way you can't see the error message any more. – RHA Jan 14 '16 at 14:43
  • @mra68 `if (TRUE) {.Internal(.dfltStop("End of script?", TRUE))}` gives `Error in TRUE : End of script?` and then continues as does stop. – RHA Jan 14 '16 at 14:44
  • @RHA: Maybe our `.dfltStop` functions are different, R version and platform coincide. Following Dominic Comtois answer to [a question about source code](http://stackoverflow.com/questions/14035506/how-to-see-the-source-code-of-r-internal-or-primitive-function) I used `pryr::show_c_source(.Internal(.dfltStop(message, call)))` to see that my `.dfltStop` is implemented by "do_dfltStop with op=0". The C code is in ["errors.c"](https://github.com/wch/r-source/blob/4017e931aa458b394178abc1d3509d32aea2105a/src/main/errors.c), lines 1757-1770. – mra68 Jan 14 '16 at 15:36
  • @mra68 I get `.dfltStop is implemented by do_dfltStop with op = 0` as well, but what it means is beyond me. – RHA Jan 14 '16 at 16:13
  • @RHA: It means that the same C function is called. Have you tried `if (NA) 0`? – mra68 Jan 14 '16 at 16:24
  • @mra68 Yep. if (TRUE) {if (NA) 0} gives an error, but `print("Script did NOT end!")` gets executed nevertheless. I am beginning to think that the whole script as a function is the best/only option. – RHA Jan 14 '16 at 16:29
  • @RHA: The error message I get is a little bit different from the one mentioned in the question Not only `Error: End of script?`, but `Error in eval(expr, envir, enclos) : End of script?`. Have you already tried to run the script without Rstudio? – mra68 Jan 14 '16 at 16:36

4 Answers4

24

maybe it is a bit too late, but I recently faced the same issue and find that the simplest solution for me was to use:

quit(save="ask")

From ?quit you can see that:

save must be one of "no", "yes", "ask" or "default". In the first case the workspace is not saved, in the second it is saved and in the third the user is prompted and can also decide not to quit. The default is to ask in interactive use but may be overridden by command-line arguments (which must be supplied in non-interactive use).

You can then decide not to quit R by clicking on "cancel" when the message box pops-up.

Hope that helps!

NonoRbrico
  • 341
  • 2
  • 4
  • 1
    It is never too late for an answer. It is indeed another workaround. 'quit' is intended to quit the entire session, but as you describe this can be cancelled. – RHA May 24 '18 at 06:34
  • 2
    In RStudio this command kills R, and RStudio prompts you to restart the session – robertspierre Sep 13 '21 at 13:24
  • Not sure if Rstudio has changed since this answer, but the user has no langer the option to cancel the quitting; Calling quit always results in termination of the session. That makes this answer less useful when you just want to stop execution of the script (for instance while debugging). – RHA Sep 09 '22 at 12:25
18

As far as I could find, there is no single command that really stops a script on every platform/version. There are several ways to handle this:

Put it in a function or curly brackets:

{
if (TRUE) {stop("The value is TRUE, so the script must end here")}

print("Script did NOT end!")
}

OR evaluate the error and handle it like in an if else construction:

if (TRUE) {stop("The value is TRUE, so the script must end here")    
  } else { #continue the script
print("Script did NOT end!")   
  }

OR (EDIT): Another possibility is to call the script from a seperate 'main' R-scipt, with source("MyScript.R"). Then the script terminates. This however, suppresses all output other then errors to the console.

OR for more complex operations, use tryCatch() as shown here

RHA
  • 3,677
  • 4
  • 25
  • 48
3

Note that in RStudio the

stop("Error message")

call actually stop the execution of the script after printing the error message.

robertspierre
  • 3,218
  • 2
  • 31
  • 46
2

Very simple: call a function that has not been declared:

condition <- TRUE
if (condition) {
  print('Reason for stopping')
  UNDECLARED()
}

And the script will stop with the messages:

[1] "Reason for stopping"
Error in UNDECLARED() : could not find function "UNDECLARED"
while_for
  • 45
  • 2