Make execution stop on error in RStudio / Interactive R session

不打扰是莪最后的温柔 提交于 2021-02-08 12:19:49

问题


When executing a block of code in RStudio, execution doesn't actually stop when an error occurs. For example, if I have the following code in the open editor:

x <- 'test'
stopifnot(is.numeric(x))
print('hello world')

And run it (either with command-return or by clicking the "Run" button), it prints the error but then marches on and executes the print statement.

Is there a way to configure RStudio to not proceed past the error? i.e. make it stop at line 2 above and not proceed to the print statement?

EDIT: Just realized this also happens if I'm sending blocks of code in the standard R GUI using command-R.


回答1:


I don't think that there is a way to prevent RStudio from running all the lines, when you select a section and press Ctrl+Enter. Rstudio is just running one line after the other. Even if stopifnot() is called inside of a function, all the lines after that function call will still be evaluated.

If your goal is simply to be informed when something goes wrong, before a lot of code is run in vain, maybe you could define a function similar to stopifnot() that will just go into an endless loop, if there is an error. You could then press Esc or the Stop-Button in RStudio to interrupt the program. Something like this:

waitifnot <- function(cond) {
  if (!cond) {
    message(deparse(substitute(cond)), " is not TRUE")  
    while (TRUE) {}
  }
}

Now, you can run your example code with this function:

x <- 'test'
waitifnot(is.numeric(x))
print('hello world')

As expected, hello world is never printed. You will get an error message, telling you that something went wrong, and then the program will wait until you abort it manually.

This won't work well in any situation other than interactive mode. To avoid unpleasant situations, you could also let the function react differently, if it is not used in interactive mode, for instance like this:

waitifnot <- function(cond) {
  if (!cond) {
    msg <- paste(deparse(substitute(cond)), "is not TRUE")
    if (interactive()) {
      message(msg)
      while (TRUE) {}
    } else {
      stop(msg)
    }
  }
}

This function will go into an endless loop only if run in interactive mode. Otherwise, it will simply abort execution by calling stop(). I have checked that this works as expected with Ctrl+Enter or the Source button in RStudio (endless loop) and with Rscript on the Bash command line (abort of the program).




回答2:


As mentioned by @lmo, you just need to hit the button Source instead of select all + Run. In fact, you don't even need to save the script file to be able to hit the Source button.

But the problem indeed is that Source will always run the entire script. So if you really only want to run a block within the script, this won't help you.



来源:https://stackoverflow.com/questions/42256291/make-execution-stop-on-error-in-rstudio-interactive-r-session

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!