2

I am creating a pdf file where I am "printing" a png with grid.draw() since it was a merge of two plots, therefore impossible to use ggsave since it was not a plot.

Anyways, when printing the result, everything is fine except the message from dev.off(). The console is as it follows:

# saving grid draw to a png (not a ggplot class, therefore other kind of saving is needed)
> png(filename = "data_x_plot.png",
+         pointsize = 500, 
+         width = 1200,
+         height = 1000,
+         units = "px") 
> grid.draw(data_x)
> dev.off()
RStudioGD 
        2 

And the code in the .Rnw script is as follows:

g1 <- ggplotGrob(p1)
g2 <- ggplotGrob(p2)
data_x <- rbind(g1, g2, size="first") # stack the two plots
data_x$widths <- unit.pmax(g1$widths, g2$widths) # use the largest widths

# saving grid draw to a png (not a ggplot class, therefore other kind of saving is needed)
png(filename = "data_x_plot.png",
    pointsize = 500, 
    width = 1200,
    height = 1000,
    units = "px") 
grid.draw(data_x)
dev.off()
@
  
  \begin{center}
\includegraphics[scale=0.37]{data_x_plot.png}
\captionof{figure}{Overview of the x data.}
\end{center}

Strangely, what is being printed to the pdf before each png.file is not "RStudioGD 2", but "pdf 2". I do not what "RStudioGD 2"to be printed out... I just want that this kind of messages do not pass to the pdf file.

Can you tell me how to avoid or how to hide this message from dev.off()?

  • 2
    Try `invisible(capture.output(dev.off()))`. – Rui Barradas Oct 21 '21 at 15:06
  • Thank you Rui. Where should I put this piece of code? Right after dev.off()? Or in replace of dev.off() ? – João Machado Oct 21 '21 at 15:12
  • 1
    In place of `dev.off()`. Right after it would first close the device and print the annoying message, then there would nothing left to close... – Rui Barradas Oct 21 '21 at 15:15
  • That worked perfectly! Omg, I dont believe I spent so much time and the solution was that easy... but I couldnt find any tip regarding invisible() function. I was only seeing things like ```{r, error=FALSE}``` and that stuff. Thank you! – João Machado Oct 21 '21 at 15:19
  • Also, as there is this code to clear R environment ```rm(list = ls())```, do you know if there is anything to clear the "plots" environment as a code like the previous one? – João Machado Oct 21 '21 at 15:23
  • From the 3rd paragraph of `?dev.off`: *"graphics.off() shuts down all open graphics devices. Normal termination of a session runs the internal equivalent of graphics.off()"*. Is this what you are looking for? It works on RGui and on RStudio. – Rui Barradas Oct 21 '21 at 16:40
  • Thnaks for the suggestion. I have read a bit about it and it seems that in the console works fine, but I wanted to put in a script to run automatically and in there, it gives some issues... https://stackoverflow.com/questions/22640016/code-to-clear-all-plots-in-rstudio – João Machado Oct 21 '21 at 16:43

1 Answers1

1

As Rui Barradas, said in the comments, the solution is just replace the

dev.off()

by

invisible(capture.output(dev.off()))

Very easy solution to omit any message created by annoying functions.