[R] close specific graphics device

Henrik Bengtsson henrik.bengtsson at gmail.com
Thu Dec 17 06:58:31 CET 2015


The R.devices package provides functions for this.  For instance, you
can open several devices with different labels and the close then in
whatever order you'd like:

> library("R.devices")
> devSet("foo")
> plot(1:10)
> devSet("bar")
> plot(10:1)
> devSet("foo")
> points(10:1)
> devSet("bar")
> devOff("foo")
> devOff("bar")

Alternatively, you can specify the 'label' argument when you use devNew(), e.g.

> devNew("x11", label="foo")
> plot(1:10)
> devNew("png", filename="myplot.png", label="bar")
> plot(10:1)
> devOff("foo")
> devOff("bar")

The R.devices package also allows you to open a device with any index
number in [2,63], e.g.

> devSet(43)

regardless of whether devices 2 to 42 already exists or not.

Also, if you don't already know, R.devices provides devEval(), e.g.

devEval("png", name="myplot", {
  plot(10:1)
})

which guarantees that the device is closed afterward, i.e. no more
forgetting to use dev.off().  Also, filename extensions etc are
automatically taken care of.  You can plot to multiple image types at
the same time, e.g.

devEval(c("png", "pdf", "eps"), name="myplot", aspectRatio=2/3, {
  plot(10:1)
})

There are also "quick" functions such as:

toPNG("myplot", aspectRatio=2/3, {
  plot(10:1)
})

Hope this helps

Henrik

PS. R.devices 2.13.2 is rolling out on CRAN right now - make sure to
use that version if your doing _unbalanced_ opening/closing with
labels as in my first example.

On Tue, Dec 15, 2015 at 2:38 PM, Jim Lemon <drjimlemon at gmail.com> wrote:
> Hi Dan,
> The range of device numbers seems to be 1-63. There doesn't appear to be a
> means of explicitly setting the device number when calling dev.new, and
> devices are numbered sequentially when they are opened. This means that
> even if you did know that the device number was, say, 4 it would be
> possible to close that device and open another device with the number 4.
>
> I suppose it would be possible to write wrapper functions for this, but I
> have to leave at the moment, so perhaps tomorrow.
>
> Jim
>
> On Wed, Dec 16, 2015 at 7:51 AM, 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!
>>
>>
>>
>> --
>> Dan Dalthorp, PhD
>> USGS Forest and Rangeland Ecosystem Science Center
>> Forest Sciences Lab, Rm 189
>> 3200 SW Jefferson Way
>> Corvallis, OR 97331
>> ph: 541-750-0953
>> ddalthorp at usgs.gov
>>
>>         [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list