R语言 全局性地抑制警告

R语言 全局性地抑制警告

在这篇文章中,我们将讨论如何在R编程语言中全面抑制警告。

警告是一种不会干扰程序流程的信息,但在输出时显示警告。为了在全局范围内抑制警告,我们必须在选项函数中设置 warn=-1。

语法

options( warn = - 1)

如果你想看到警告,则设置 warn=0。

例子: 使用pmax和pmin时查看警告的R程序

# pmax function and display the warnings
# pmax function will return the parallel
# maximum of two vectors
pmax(c(1, 2, 3), c(1, 2, 3, 4, 5))
  
# pmin function and display the warnings
# pmin function will return the parallel
# minimum of two vectors
pmin(c(1, 2, 3), c(1, 2, 3, 4, 5))

输出

在R语言中全局性地抑制警告

例子: R程序抑制了使用pmax和pmin时的警告信息。

# suppress the warnings by setting warn=-1
options(warn=-1)
  
# pmax function and display the warnings
# pmax function will return the parallel
# maximum of two vectors
pmax(c(1, 2, 3), c(1, 2, 3, 4, 5))
  
# pmin function and display the warnings
# pmin function will return the parallel
# minimum of two vectors
pmin(c(1, 2, 3), c(1, 2, 3, 4, 5))

输出

12345
12312

如果我们想再次看到警告,设置 warn=0。

例子: 用R程序再次查看警告信息

# display the warnings by setting warn=0
options(warn=0)
  
# pmax function and display the warnings
# pmax function will return the parallel
# maximum of two vectors
pmax(c(1, 2, 3), c(1, 2, 3, 4, 5))
  
# pmin function and display the warnings
# pmin function will return the parallel
# minimum of two vectors
pmin(c(1, 2, 3), c(1, 2, 3, 4, 5))

输出

在R语言中全局性地抑制警告

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程