如何将warnings()输出到字符串 [英] How to get warnings() output to string
问题描述
当我输入warnings()进行控制台时,我回来了
When I type warnings() to console, I get back
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())
时,我得到:
However when I type as.character(warnings())
, I get:
[1] "fread(my_directory)"
我的目标是将warning()中显示的 actual 消息转换为字符串,以便将其传递给 logwarn
函数.记录包.目前,我正在执行 logwarn(warnings(),logger ="some_log_file.log")
来记录我的警告,但它给我在上面显示的 character
施加了不正确的强制性
My objective is to get the actual message displayed in warning() into a character string, so that I can pass it to the logwarn
function in the logging
package. Currently, I am doing logwarn(warnings(),logger="some_log_file.log")
to record my warnings, but it gives the incorrect coercion to character
that I displayed above.
请注意,我只能使用 sink
,但是我想坚持使用 logging
包,因此我需要能够将强制性更正为 character
Note that I can just use sink
but I want to stick with logging
package, so I require the ability to correct coerce to character
.
推荐答案
这可能不是您要找的确切答案,但我认为值得一提.
This may not be the exact answer you're looking for, but I think it's worth a mention.
R有一个全局变量 last.warning
,它仅包含最后一个警告.在其上调用 names
将返回最后的警告作为字符串.这是一个小例子
R has a global variable, last.warning
, which holds just that, the last warning. Calling names
on it will return the last warning as a character string. Here's a little example
首先,故意触发警告:
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"
现在查看名称(last.warning)
.这将以字符串形式返回警告:
Now look at the names(last.warning)
. This returns the warning as a character string:
names(last.warning)
# [1] "the condition has length > 1 and only the first element will be used"
这篇关于如何将warnings()输出到字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!