How to suppress qplot's binwidth warning inside a function?
To attempt to clear up some confusion, this construct does not prevent the binwidth warnings/messages to appear:
suppressMessages(p <- ggplot(...))print(p)
But this does:
p <- ggplot(...)suppressMessages(print(p))
As Hadley's comment points out, lazy evaluation prevents the stat_*
functions from actually running until they need to at print-time.
I can't explain the why of this one (Hadley may swing by and do so) but using ggplot
instead of qplot
solves the problem:
d <- data.frame(v1 = rnorm(100))myfun <- function(x){ p <- ggplot(data = x, aes(x = v1)) + geom_histogram(binwidth = diff(range(x$v1))/30) print(p)}
Doing it this way I get no warning message. Also, using ggplot
and removing the binwidth = ...
portion in geom_histogram
makes the warning reappear, but then suppressMessages
works as expected as well.
I suspect this has to do with namespaces or environments and when/where qplot
and ggplot
are evaluating arguments. But again, that's just a guess...
As they say on TV "Had this been a real warning you would have been given directions from your local authorities."
Since it wasn't a warning then my original answer didn't cause it to error out. This is what I should have written:
options(warnings= -1)<do something> # no warningsoptions(warnngs=1)<business as usual>
But it wasn't a warning but a message to the console. Here's how to stop it:
con=file("temp.fil", "w") sink(con, type="message") library(ggplot2) d=rnorm(100) myfun=function(x) qplot(x) myfun(d) sink( type="message")