How to suppress warnings globally in an R Script

2019-01-07 04:27发布

I have a long R script that throws some warnings, which I can ignore. I could use

suppressWarnings(expr)

for single statements. But how can I suppress warnings in R globally? Is there an option for this?

标签: r warnings
4条回答
对你真心纯属浪费
2楼-- · 2019-01-07 04:36

Have a look at ?options and use warn:

options( warn = -1 )
啃猪蹄的小仙女
3楼-- · 2019-01-07 04:42

You could use

options(warn=-1)

But note that turning off warning messages globally might not be a good idea.

To turn warnings back on, use

options(warn=0)

(or whatever your default is for warn, see this answer)

查看更多
我欲成王,谁敢阻挡
4楼-- · 2019-01-07 04:42

You want options(warn=-1). However, note that warn=0 is not the safest warning level and it should not be assumed as the current one, particularly within scripts or functions. Thus the safest way to temporary turn off warnings is:

oldw <- getOption("warn")
options(warn = -1)

[your "silenced" code]

options(warn = oldw)
查看更多
地球回转人心会变
5楼-- · 2019-01-07 04:52

I have replaced the printf calls with calls to warning in the C-code now. It will be effective in the version 2.17.2 which should be available tomorrow night. Then you should be able to avoid the warnings with suppressWarnings() or any of the other above mentioned methods.

登录 后发表回答