【问题标题】:Catch specific warning and ignore others [duplicate]捕捉特定警告并忽略其他警告[重复]
【发布时间】:2016-05-04 16:04:41
【问题描述】:

我正在调试一些给出多个警告的代码,但我试图在收到特定警告时停止代码,以便查看环境。

例如:

myfun <- function(){
  warning("The wrong warning")
  warning("The right warning")
  print("The end of the function")
}

tryCatch(myfun(),
         warning = function(w){
           if(grepl("right", w$message)){
             stop("I have you now")
           } else {
             message(w$message)
           }
         })

我希望函数在“正确的警告”处停止,但一旦收到第一个警告,catch 就会停止。如何跳过不感兴趣的警告并停止关注我感兴趣的警告?

【问题讨论】:

    标签: r try-catch


    【解决方案1】:

    我相信withCallingHandlers是你想要的:Disregarding simple warnings/errors in tryCatch()

    withCallingHandlers(myfun(),
       warning = function(w){
         if(grepl("right", w$message)){
           stop("I have you now")
         } else {
           message(w$message)
         }
       })
    

    【讨论】:

    • 那已经死了。我要将我的问题标记为重复。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-29
    • 2014-03-15
    相关资源
    最近更新 更多