Skip to content

Instantly share code, notes, and snippets.

@Nicktz
Last active March 3, 2016 12:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Nicktz/c948240c33b74a7ade7d to your computer and use it in GitHub Desktop.
Save Nicktz/c948240c33b74a7ade7d to your computer and use it in GitHub Desktop.
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