How do you delete the current (but not all) plots in the RStudio plotting device?

Hillary Sanders picture Hillary Sanders · Jun 23, 2015 · Viewed 16.5k times · Source

How do you delete the current (but not all) plots in the RStudio plotting device?

dev.off() will remove all plots, but what if I just want to remove one? I don't want to have to press that red 'x' button because I want to remove one plot without pressing a button.

Answer

haitham picture haitham · Jun 24, 2015

In R, you would just use dev.new() before each plot, so you dev.off() to only clear the last plot.

In RStudio, you can use x11(), windows() or quartz() (depending on your device) before each plot. Then call dev.off() to clear last plot. You can also use dev.set() to choose specific plots that way.

If your question is specifically asking to delete the last plot within the same RStudio window (rather than making new windows), not sure if it's possible, since RStudio treats that window as one device. An idea would be to look at a way to call the C++ function removePlot() in the RStudio project.

I found in the Github repository for RStudio the C++ code:

display.removePlot(display.activePlotIndex());

You could output the plots and manage the files that way.