首页 > 解决方案 > Additionnal warning message returns an error instead of a warning

问题描述

I have two functions, f_ that throws an error and f that throws a warning before calling f_.

f_ <- function() stop()
f <- function() {
  warning()
  f_()
}

Since I have a warning before the error, R produces "additionnal warning messages", but the message in this warning is not my f warning but the error produced in f_ called a 2nd time :

> f()
Error in f_() : 
In addition: Warning message:
In f() :
  Error in f_() :

It seems to works as expected if the error is produced in the same function or by a built_in function.

f <- function() {
  warning()
  stop()
}
> f()
Error in f() : 
In addition: Warning message:
In f() : 

Can someone helps me to understand what is happening there ? Thanks for any help. I'm running R version 3.3.2 on x86_64-w64-mingw32 using RStudio.

标签: rrstudio

解决方案


我认为这是由 Rstudio 错误检查器引起的。当遇到错误时,Rstudio 会显示回溯和调试的可能性。我相信这是混乱的根源(包括我自己)。“第二个”错误只是 Rstudio 中的一个功能,它有助于调试,如下所示。请注意右侧的两个按钮,允许您“显示回溯”和“重新运行调试”。

在工作室 在此处输入图像描述

正如您在下面看到的,如果您在终端中运行 R,则不存在此“附加”错误。

在一个终端 在此处输入图像描述

在 Rstudio 的全局选项中,在常规选项卡下,您可以关闭调试错误处理程序的使用。您也可以在 Debug -> On Error 下执行此操作。然后,Rstudio 将不会显示“附加”消息。

编辑: 经过进一步调查,发生了一些奇怪事情。下面,我尝试通过以下观察使错误和警告消息更具信息性:

  • f()连续多次调用,我并不完全清楚错误检查器何时出现以及何时不出现。
  • 当错误检查器确实出现时,不会显示警告消息。当错误检查器未出现时,将显示警告消息。

我对 Rstudio 的内部结构一无所知,但肯定是错误检查器导致了这些小问题。

在此处输入图像描述


推荐阅读