r - 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.
解决方案
我认为这是由 Rstudio 错误检查器引起的。当遇到错误时,Rstudio 会显示回溯和调试的可能性。我相信这是混乱的根源(包括我自己)。“第二个”错误只是 Rstudio 中的一个功能,它有助于调试,如下所示。请注意右侧的两个按钮,允许您“显示回溯”和“重新运行调试”。
正如您在下面看到的,如果您在终端中运行 R,则不存在此“附加”错误。
在 Rstudio 的全局选项中,在常规选项卡下,您可以关闭调试错误处理程序的使用。您也可以在 Debug -> On Error 下执行此操作。然后,Rstudio 将不会显示“附加”消息。
编辑: 经过进一步调查,发生了一些奇怪的事情。下面,我尝试通过以下观察使错误和警告消息更具信息性:
f()
连续多次调用,我并不完全清楚错误检查器何时出现以及何时不出现。- 当错误检查器确实出现时,不会显示警告消息。当错误检查器未出现时,将显示警告消息。
我对 Rstudio 的内部结构一无所知,但肯定是错误检查器导致了这些小问题。
推荐阅读
- unit-testing - 使用 Coroutines 和 Retrofit 进行单元测试
- django - 自定义模板加载器 Django
- azure-cosmosdb-mongoapi - 使用 $elemMatch 和 $in 时不支持 Azure CosmosDB 操作
- kubernetes - 如何使用 helm chart test 进行集成测试?
- c# - 如何使用 System.XML 在 C# 中逐行注释掉 XML 文件
- java - 无法捕获有关音频剪辑的异常
- android - CMake 错误:与 CMake 服务器通信时发生错误
- rxjs - 如何使用 Jest 在 Redux-saga 中测试 RxDB?
- java - 如何检索推送键值并更新键值的子值
- amazon-web-services - AWS Lambda 似乎在完成之前退出