84

I have code to clear the workspace: rm(list=ls()) and code to clear the console: cat("\014")

Is there code to clear all plots from Rstudio?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
dpel
  • 1,954
  • 1
  • 21
  • 31

5 Answers5

88

dev.off() closes the current graphical device. This clears all of the plots for me in RStudio as long as I don't have a different graphical device open at the moment. If you do have other graphical devices open then you can use dev.list() to figure out which graphical device is RStudio's. The following should do that but I haven't tested it thoroughly.

dev.off(dev.list()["RStudioGD"])

But if you aren't doing anything else then just using dev.off() should take care of it.

Dason
  • 60,663
  • 9
  • 131
  • 148
  • 7
    A caveat about the accepted answer. This works without a hitch in RStudio's console, but we see strange results when using it in a script (for instance, along with the other items OP mentioned, to scrub the environment each time a particular script is run). After a second run, you can't click backwards through a list of generated plots without failures & warnings about not being able to open connections or find temp files; it seems like this doesn't flush the "plots" history when run from script? – mojoronomous Oct 21 '15 at 18:46
  • @mojoronomous Works fine for me. Running RStudio 0.99.484 on Ubuntu. – Dason Oct 22 '15 at 01:46
  • (Mac OSX Yosemite & Windows 7, RStudio 0.99.486) – mojoronomous Oct 21 '15 at 18:46
  • This didn't help when working on a R markdown in RStudio (v 1.1.463, Linux x86_64). plots were still resident. `Run` -> `Restart R and clear output` did the trick. – mjkrause Dec 26 '18 at 16:35
43

dev.off() only works in an interactive session. If you're interested in implementing such behavior in a script, you should use

graphics.off()

instead.

nbro
  • 15,395
  • 32
  • 113
  • 196
Waldir Leoncio
  • 10,853
  • 19
  • 77
  • 107
  • 1
    This does not seem to be an improvement. In a script both graphics.off() and dev.off() lead here to exactly the same problems/errors as mojoronomous has described. (RStudio 1.0.44, R 3.3.2) – g g Dec 25 '16 at 13:45
  • Excellent solution! This fixed an empty square plot in my graphics. Thanks! – Joao Fonseca Aug 05 '22 at 12:03
9

To prevent error message in case there are no plots to clear:

if(!is.null(dev.list())) dev.off()
johnnyRose
  • 7,310
  • 17
  • 40
  • 61
  • Although this solution works, it results in an error when cycling through the plots during the second run. Bug reported here: https://github.com/rstudio/rstudio/issues/3117 – Nav Jul 09 '18 at 17:46
7

I usually use

while (dev.cur()>1) dev.off()

and since I use RGL a lot, I often add:

while (rgl.cur()) rgl.close()
Tom
  • 171
  • 1
  • 3
6

I have gone for this which seems to work without reporting any errors:

# Clear all plots
try(dev.off(dev.list()["RStudioGD"]),silent=TRUE)
try(dev.off(),silent=TRUE)

I merged the instructions from the other answers with an answer on error handling here:

MattG
  • 5,589
  • 5
  • 36
  • 52