Stop an R program without error
Is there any way to stop an R program without error?
For example I have a big source, defining several functions and after it there are some calls to the functions. It happens that I edit some function, and want the function definitions to be updated in R environment, but they are not actually called.
I defined a variable justUpdate
and when it is TRUE
want to stop the program just after function definitions.
ReadInput <- function(...) ...
Analyze <- function(...) ...
WriteOutput <- function(...) ...
if (justUpdate)
stop()
# main body
x <- ReadInput()
y <- Analyze(x)
WriteOutput(y)
I have called stop()
function, but the problem is that it prints an error message.
ctrl+c is another option, but I want to stop the source in specific line.
The problem with q()
or quit()
is that it terminates R session, but I would like to have the R session still open.
As @JoshuaUlrich proposed browser()
can be another option, but still not perfect, because the source terminates in a new environment (i.e. the R prompt will change to Browser[1]>
rather than >
). Still we can press Q
to quit it, but I am looking for the straightforward way.
Another option is to use if (! justUpdate) { main body }
but it's clearing the problem, not solving it.
Is there any better option?
I found a rather neat solution here. The trick is to turn off all error messages just before calling stop()
. The function on.exit()
is used to make sure that error messages are turned on again afterwards. The function looks like this:
stop_quietly <- function() {
opt <- options(show.error.messages = FALSE)
on.exit(options(opt))
stop()
}
The first line turns off error messages and stores the old setting to the variable opt
. After this line, any error that occurs will not output a message and therfore, also stop()
will not cause any message to be printed.
According to the R help,
on.exit
records the expression given as its argument as needing to be executed when the current function exits.
The current function is stop_quietly()
and it exits when stop()
is called. So the last thing that the program does is call options(opt)
which will set show.error.messages
to the value it had, before stop_quietly()
was called (presumably, but not necessarily, TRUE
).
There is a nice solution in a mailing list here that defines a stopQuietly
function that basically hides the error shown from the stop
function:
stopQuietly <- function(...) {
blankMsg <- sprintf("\r%s\r", paste(rep(" ", getOption("width")-1L), collapse=" "));
stop(simpleError(blankMsg));
} # stopQuietly()
> stopQuietly()
I have a similar problem and, based on @VangelisTasoulas answer, I got a simple solution.
Inside functions, I have to check if DB is updated. If it is not, stop the execution.
r=readline(prompt="Is DB updated?(y/n)")
Is DB updated?(y/n)n
if(r != 'y') stop('\r Update DB')
Update DB
Just putting \r
in the beginning of the message, overwrite Error:
in the message.
You're looking for the function browser
.
You can use the following solution to stop an R program without error:
if (justUpdate)
return(cat(".. Your Message .. "))