break/exit script
Perhaps you just want to stop executing a long script at some point. ie. like you want to hard code an exit() in C or Python.
print("this is the last message")
stop()
print("you should not see this")
Edited. Thanks to @Droplet, who found a way to make this work without the .Internal()
: Here is a way to implement an exit()
command in R.
exit <- function() { invokeRestart("abort") }
print("this is the last message")
exit()
print("you should not see this")
Only lightly tested, but when I run this, I see this is the last message
and then the script aborts without any error message.
Below is the uglier version from my original answer.
exit <- function() {
.Internal(.invokeRestart(list(NULL, NULL), NULL))
}
You could use the stopifnot()
function if you want the program to produce an error:
foo <- function(x) {
stopifnot(x > 500)
# rest of program
}