How can I check whether a function call results in a warning?

前端 未结 4 1002
情话喂你
情话喂你 2020-11-29 23:00

In R, how can I determine whether a function call results in a warning?

That is, after calling the function I would like to know whether that instance of the call yi

4条回答
  •  盖世英雄少女心
    2020-11-29 23:35

    here is an example:

    testit <- function() warning("testit") # function that generates warning.
    
    assign("last.warning", NULL, envir = baseenv()) # clear the previous warning
    
    testit() # run it
    
    if(length(warnings())>0){ # or !is.null(warnings())
        print("something happened")
    }
    

    maybe this is somehow indirect, but i don't know the more straightforward way.

提交回复
热议问题