简体   繁体   English

关闭 R 笔记本中的警告

[英]Turn off warnings in R Notebook

I am working with Jupyter in R environment.我正在 R 环境中使用 Jupyter。 I hate to see warnings which came from different R packages.我讨厌看到来自不同 R 包的警告。 In order to do that I try same line from Python code:为了做到这一点,我尝试了 Python 代码中的同一行:

import warnings
warnings.filterwarnings('ignore') 

But warnings are still there.但是警告仍然存在。 So can anybody help me how to solve this problem?那么有人可以帮我解决这个问题吗?

You can use options(warn=-1) in an R Script to turn off warning messages.您可以在 R 脚本中使用options(warn=-1)来关闭警告消息。 But this might not be an good idea.但这可能不是一个好主意。

To turn warnings back on, use要重新打开警告,请使用

options(warn=0)

Hope this solves your problem.希望这能解决您的问题。

While you can turn off warnings globally, it's usually not a good idea.虽然您可以全局关闭警告,但这通常不是一个好主意。 Fortunately, there are ways to suppress output for specific instances.幸运的是,有一些方法可以针对特定实例抑制 output。

Some alternatives that work in a Jupyter environment are:在 Jupyter 环境中工作的一些替代方案是:

# suppress conflict warnings at package load
library(dplyr, warn.conflicts=FALSE)

# suppress startup messages at package load
suppressPackageStartupMessages(library(tidyverse))

# suppress warnings
suppressWarnings( as.numeric(c('0', '1', '2', 'three')) )

# suppress messages
suppressMessages(df %>% group_by(location) %>% summarize(revenue))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM