[R] close specific graphics device

Sarah Goslee sarah.goslee at gmail.com
Tue Dec 15 22:08:01 CET 2015

You could keep track of the device number using dev.cur() when you
create the plot, and some combination of

to manage them.


On Tue, Dec 15, 2015 at 3:51 PM, Dalthorp, Daniel <ddalthorp at usgs.gov> wrote:
> dev.off(which) can be used to close a specific graphics device where
> "which" is the index of the device, but is there a way to assign a custom
> number (or name) to a windows device so that specific window can be later
> closed via dev.off (or some other method) if it is open?
> The following does NOT work. The target device is not open when its dev.off
> is called, and another window that later got assigned the original index
> associated with the target device is closed instead.
> plot(0,0,type='n') # target window to close
> text(0,0,"close me")
> targetindex<-dev.cur()
> # unbeknownst to the programmer, user closes device by clicking the red "X"
> or...
> dev.off()
> # user draws a new graph that he wants to keep open
> plot(1,1,type='n')
> text(1,1,"do not close me")
> # now it's time for the program to close the original graphics device (if
> it still happens to be open)
> dev.off(targetindex)
> # the wrong device has been closed because the original window had closed
> and the index associated with original graph is now associated with
> something else
> ----
> I'm looking for something like:
> dev.off(which = "original figure") or dev.off(which = n), where n is a
> custom index (like 10000) that will not be later assigned to a different
> device [unless explicitly assigned that index].
> Any help would be greatly appreciated.
> Thanks!

Sarah Goslee

More information about the R-help mailing list