Skip to content

Instantly share code, notes, and snippets.

@yihui
Created September 22, 2013 04:05
Show Gist options
  • Save yihui/6656584 to your computer and use it in GitHub Desktop.
Save yihui/6656584 to your computer and use it in GitHub Desktop.
this shows the correct way to suppress warnings is suppressWarnings() instead of options(warn = -1) alone; similarly, you should use suppressMessages() to suppress messages
options(warn = -1)
# cannot really suppress warnings from the root level
withCallingHandlers(warning("hi"), warning = function(w) {
print(w)
})
# the warning can still be captured:
## <simpleWarning in withCallingHandlers(warning("hi"), warning = function(w) { print(w)}): hi>
# however, this always works
withCallingHandlers(suppressWarnings(warning("hi")), warning = function(w) {
print(w)
})
# even if when warn=0
options(warn = 0)
withCallingHandlers(suppressWarnings(warning("hi")), warning = function(w) {
print(w)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment