Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop an R program without error

Tags:

r

debugging

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?

like image 696
Ali Avatar asked Jan 22 '13 22:01

Ali


People also ask

How do I stop an execution of a program in R?

The shortcut to interrupt a running process in R depends on the R software and the operating system you are using. However, if you are using RStudio on a Windows computer, you can usually use Esc to stop a currently executing R script. Then, we can press Esc to interrupt the loop.

What does stop () do in R?

The stop R function generates an error message and stops executing the current R code.

How do you stop a python function from running?

To stop code execution in python first, we have to import the sys object, and then we can call the exit() function to stop the program from running. It is the most reliable way for stopping code execution. We can also pass the string to the Python exit() method.


3 Answers

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).

like image 104
Stibu Avatar answered Oct 16 '22 09:10

Stibu


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()
like image 15
Vangelis Tasoulas Avatar answered Oct 16 '22 10:10

Vangelis Tasoulas


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.

like image 10
xm1 Avatar answered Oct 16 '22 10:10

xm1