Last active
March 3, 2016 12:32
-
-
Save Nicktz/c948240c33b74a7ade7d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Adjusting the documentation of ?suppressWarning to only suppress a very specific warning, e.g. if we know a function will warn about zero standard deviation: | |
foo <- function() { | |
x <- cor( c(1,1,1,1,1),c(1:5),method = 'spearman',use = "pairwise.complete.obs") | |
} | |
x <- foo() # This produces the warning: "the standard deviation is zero" | |
To suppress this warning when running foo(), wrap it with the following function: | |
suppressSpecificWarning <- function(expr, messages = character()) | |
{ | |
opts <- options(warn = -1) | |
on.exit(options(opts)) | |
withCallingHandlers(expr, warning=function(w) { | |
if (conditionMessage(w) %in% messages) | |
invokeRestart("muffleWarning") | |
}) | |
} | |
x <- suppressSpecificWarning( foo(), messages = "the standard deviation is zero") # No warning | |
x <- foo() # Warning |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment