📜  在 R 中全局抑制警告

📅  最后修改于: 2022-05-13 01:55:01.940000             🧑  作者: Mango

在 R 中全局抑制警告

在本文中,我们将讨论如何在 R 编程语言中全局抑制警告。

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

句法:

如果您想查看警告,请设置 warn=0



示例: R 程序在使用 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
# 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))


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))


输出:

示例:使用 pmax 和 pmin 时抑制警告消息的 R 程序

电阻

# 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))

输出: