[R] filename of current device

Andreas Leha andreas.leha at med.uni-goettingen.de
Mon May 19 10:06:26 CEST 2014



On 18/05/2014 18:08, Prof Brian Ripley wrote:
> 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.
> 


Very nice!  Thank you for pointing that out.  That's exactly what I was
hoping to see in R.  Good to know that this is already there.

Regards,
Andreas



More information about the R-help mailing list