R`options(warning.expression = <non-null>)`替换函数无条件删除args

时间:2019-01-03 00:22:33

标签: r

我想为打印警告自定义R的前缀。 R的warning.expression的标准设置为NULL,因此可以正常工作

options(warning.expression=NULL)
warning("this is a test of the warning system")
Warning message:
this is a test of the warning system
warning(msg="this is a test of the warning system")
Warning message:
this is a test of the warning system
warning(xkqp="this is a test of the warning system")
Warning message:
this is a test of the warning system

如果在Rscript中在短时间内(或在函数/关闭中?)遇到多个警告,您将得到类似的信息

Warning messages:
1. this is a test of the warning system
2. second message

但是,警告文本的自定义似乎不可自定义(在warning()调用中不会破坏消息)。例如,如果我将warning.expression替换如下:

mywarn<-function(...) { 
   print(paste(
     names(substitute(alist(...))),
     force(substitute(alist(...))),
     sep=" = ", 
     collapse=";"
   ))
}
options(warning.expression=quote(mywarn(...)))
warning("this is a test of the warning system")
[1] " = alist;muffleWarning = function() NULL"
warning(msg="this is a test of the warning system")
[1] " = alist;muffleWarning = function() NULL"

而直接使用mywarn()

mywarn("this is a test of the warning system", named_parameter="see")
[1] " = alist; = this is a test of the warning system;named_parameter = see"

我对warning.expression的解释最接近的东西来自a mailing list 13 years ago。 该线程中最大的帮助来自Barry Rowlingson的帖子,暗示R团队在C源代码中的实现不完整。

R文档本身是不够的。

  

warning.expression:如果出现警告,则将调用R代码表达式   生成,替换标准消息。如果非null,则称为   不论期权警告的价值如何。

0 个答案:

没有答案