Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I call ggsave without producing any output like `Saving 7 x 7 in image`?

Tags:

r

ggplot2

I try to suppress the Saving 7 x 7 in image output of ggsave() whenever I run it, but it seems impossible. Is this possible? How to do it?

I have tried the following, but nothing works:

  • capture.output()
  • sink()
  • changing the warnings option
  • assign it to a variable

Minimal ``working'' example:

librrary(ggplot2)
df <- data.frame(gp = factor(rep(letters[1:3], each = 10)),
                      y = rnorm(30))
plot.to.be.saved <- ggplot(df) + geom_point(aes(x = gp, y = y))
sink('/dev/null')
ggsave(filename = '~/.so.pdf', plot = plot.to.be.saved)
sink()
# Saving 7 x 7 in image

options(warn=-1)

no.output.please <- ggsave(filename = '~/.so.pdf', plot = plot.to.be.saved)
# Saving 7 x 7 in image
capture.output(ggsave(filename = '~/.so.pdf', plot = plot.to.be.saved), file = 'NUL')
# Saving 7 x 7 in image
like image 556
Konstantinos Avatar asked Aug 25 '16 19:08

Konstantinos


2 Answers

ggplot2:::plot_dim issues this as a message, so suppressMessages(ggsave("test.pdf", ggplot())) should do.

like image 89
baptiste Avatar answered Oct 23 '22 21:10

baptiste


If you get a message like:

Saving 6.45 x 7.47 in image

ggsave() is telling you how large it rendered the image because no size values were specified.

Add the following to ggsave() to prevent this message:

width=w, height=h
like image 12
jim r Avatar answered Oct 23 '22 20:10

jim r