如何从R中的自写函数隐藏警告消息?

问题描述:

我正在运行一个普通的R脚本,其中我有一个自写功能。该功能使用rm()这常常会产生警告,我不希望出现在控制台输出中。所有这些解决方案:从rm使用如何从R中的自写函数隐藏警告消息?

  1. 隐藏的警告从这个特定的自写功能,
  2. 隐藏警告从rm所有使用(全球范围内为R对话)

会满足我。

foo.function <- function(){ 
    rm(foo.object) 
    print("foo") 
} 

foo.function() 
# [1] "foo" 
# Warning message: 
# In rm(foo.object) : object 'foo.object' not found 
+1

你试过使用'suppressWarnings()'吗? –

+0

当然我没有:(这是正确的,谢谢! –

+1

你可以做一些像'if(length(ls(pattern ='foo.object'))== 1)rm(foo.object)'。 – steveb

对于对象的这些特殊情况下没有找到,你可以使用这样的事情:

if(exists("foo.object")) rm(foo.object) 

如果你想隐藏其它警告以及,只需使用suppressWarnings()。