当我在控制台输入 warnings() 时,我回来了
Warning message:
In fread(my_directory, ... :
C function strtod() returned ERANGE for one or more fields. The first was string input '4.40589099726375E-309'. It was read using (double)strtold() as numeric
但是当我输入时as.character(warnings())
, I get:
[1] "fread(my_directory)"
我的目标是获得actualwarning() 中显示的消息转换为字符串,以便我可以将其传递给logwarn
函数在logging
包裹。目前,我正在做logwarn(warnings(),logger="some_log_file.log")
记录我的警告,但它给了不正确的强制character
我上面显示的。
请注意,我可以只使用sink
但我想坚持logging
包,所以我需要能够纠正强制character
.
这可能不是您正在寻找的确切答案,但我认为值得一提。
R有一个全局变量,last.warning
,其中包含最后一个警告。呼唤names
它将以字符串形式返回最后一个警告。这是一个小例子
首先,故意触发警告:
x <- 1:5
if(x == 1) "yes" else "no"
# [1] "yes"
# Warning message:
# In if (x == 1) "yes" else "no" :
# the condition has length > 1 and only the first element will be used
查看变量last.warning
:
last.warning
# $`the condition has length > 1 and only the first element will be used`
# if (x == 1) "yes" else "no"
现在看看names(last.warning)
。这将警告作为字符串返回:
names(last.warning)
# [1] "the condition has length > 1 and only the first element will be used"
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)