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
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. – Anthropogeographyq()
? On my system it breaks down the whole R session. Perhaps put yoursessionInfo()
into the question. – Trysailif (TRUE) {.Internal(.dfltStop("End of script?", TRUE))}
to stop the script. Perhaps you have a different internal function.dfltStop
. – Trysailif (TRUE) {if (NA) 0}
. It also stops the script. – Trysailif (TRUE) {.Internal(.dfltStop("End of script?", TRUE))}
givesError in TRUE : End of script?
and then continues as does stop. – Imperium.dfltStop
functions are different, R version and platform coincide. Following Dominic Comtois answer to a question about source code I usedpryr::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", lines 1757-1770. – Trysail.dfltStop is implemented by do_dfltStop with op = 0
as well, but what it means is beyond me. – Imperiumif (NA) 0
? – Trysailprint("Script did NOT end!")
gets executed nevertheless. I am beginning to think that the whole script as a function is the best/only option. – ImperiumError: End of script?
, butError in eval(expr, envir, enclos) : End of script?
. Have you already tried to run the script without Rstudio? – Trysail