I have a program that does some data analysis and is a few hundred lines long.
Very early on in the program, I want to do some quality control and if there is not enou
Not pretty, but here is a way to implement an exit()
command in R which works for me.
exit <- function() {
.Internal(.invokeRestart(list(NULL, NULL), NULL))
}
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.
You can use the pskill
function in the R
"tools" package to interrupt the current process and return to the console. Concretely, I have the following function defined in a startup file that I source at the beginning of each script. You can also copy it directly at the start of your code, however. Then insert halt()
at any point in your code to stop script execution on the fly. This function works well on GNU/Linux and judging from the R
documentation, it should also work on Windows (but I didn't check).
# halt: interrupts the current R process; a short iddle time prevents R from
# outputting further results before the SIGINT (= Ctrl-C) signal is received
halt <- function(hint = "Process stopped.\n") {
writeLines(hint)
require(tools, quietly = TRUE)
processId <- Sys.getpid()
pskill(processId, SIGINT)
iddleTime <- 1.00
Sys.sleep(iddleTime)
}