if (FALSE) {
myfun <- function(x, warn=FALSE){
message('This function is rather chatty')
cat("It even prints in different output forms!\n")
message('And even at different ')
cat(" many times!\n")
cat("Too many messages can be annoying \n")
if(warn)
warning('It may even throw warnings ')
x
}
out <- myfun(1)
out
# tell the function to shhhh
out <- quiet(myfun(1))
out
# same default behaviour as quiet(), but potential for nuance
out2 <- manageMessages(myfun(1))
identical(out, out2)
# allow some messages to still get printed
out2 <- manageMessages(myfun(1), allow = "many times!")
out2 <- manageMessages(myfun(1), allow = "This function is rather chatty")
# note: . matches single character (regex)
out2 <- manageMessages(myfun(1), allow = c("many times.",
"This function is rather chatty"))
# convert specific message to warning
out3 <- manageMessages(myfun(1), message2warning = "many times!")
identical(out, out3)
# other warnings also get through
out3 <- manageMessages(myfun(1, warn=TRUE), message2warning = "times!")
identical(out, out3)
# convert message to error
manageMessages(myfun(1), message2error = "m... times!")
# multiple message intensity changes
manageMessages(myfun(1),
message2warning = "It even prints in different output forms",
message2error = "many times!")
manageMessages(myfun(1),
allow = c("This function is rather chatty",
"Too many messages can be annoying"),
message2warning = "It even prints in different output forms",
message2error = "many times!")
}
Run the code above in your browser using DataLab