在 RStudio/Interactive R 会话中出现错误时停止执行 [英] Make execution stop on error in RStudio / Interactive R session

查看:56
本文介绍了在 RStudio/Interactive R 会话中出现错误时停止执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 RStudio 中执行代码块时,在发生错误时实际上不会停止执行.例如,如果我在打开的编辑器中有以下代码:

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.

有没有办法将 RStudio 配置为不继续错误?即让它停在上面的第 2 行而不继续执行打印语句?

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?

刚刚意识到如果我使用 command-R 在标准 R GUI 中发送代码块也会发生这种情况.

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

推荐答案

当您选择一个部分并按 Ctrl+Enter 时,我认为没有办法阻止 RStudio 运行所有行.Rstudio 只是一行一行地运行.即使 stopifnot() 在函数内部被调用,该函数调用之后的所有行仍将被评估.

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.

如果你的目标只是在出现问题时得到通知,在大量代码白白运行之前,也许你可以定义一个类似于 stopifnot() 的函数,它只会进入一个无限循环,如果有错误.然后您可以按 Esc 或 RStudio 中的停止按钮来中断程序.像这样:

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

正如预期的那样,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)
    }
  }
}

这个函数只有在交互模式下才会进入无限循环.否则,它将通过调用 stop() 简单地中止执行.我已经检查过使用 Ctrl+Enter 或 RStudio 中的源按钮(无限循环)和 Bash 命令行上的 Rscript(程序中止)是否按预期工作.

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

这篇关于在 RStudio/Interactive R 会话中出现错误时停止执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆