[R] filename of current device

Prof Brian Ripley ripley at stats.ox.ac.uk
Sun May 18 19:08:23 CEST 2014


On 14/05/2014 23:04, Andreas Leha wrote:
> Hi all,
>
> how do I find out about the filename, the currently open (let's say pdf)
> device is writing to?
>
> If I find 'dev.cur()' returning 'pdf 3' when I expect 'nulldevice 1' I
> would like to know, which file that pdf device is actually targeting.
>
> Any help for my poor organization is highly appreciated...

You cannot know this for an arbitrary device, at least not from R.

Most files in R are opened with the C call fopen() and thereafter 
referred to by a file handle: the file can be renamed, unlinked ... 
whilst the handle is open.  Some system utilities[*] may be able to tell 
you what filepaths are/were associated with file handles open in a 
process, but it cannot be done portably.

However, in R-devel there is the possibility of a device recording extra 
information about itself.  See the NEWS item

     • Graphics devices can add attributes to their description in
       .Device and .Devices.  Several of those included with R use a
       "filepath" attribute.

AFAIR the pdf() device does: let's see

 > pdf('/tmp/foo.pdf')
 > dev.cur()
pdf
   2
 > .Devices
[[1]]
[1] "null device"

[[2]]
[1] "pdf"
attr(,"filepath")
[1] "/tmp/foo.pdf"


There is at least one exception: for historical reasons locked in by 
undocumented use in package tkrplot, the win.metafile() device does 
include the file path in the device 'name'.

[*] They have existed at least on Linux, Windows and OS X.

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



More information about the R-help mailing list