[R] filename of current device

David Winsemius dwinsemius at comcast.net
Thu May 15 01:55:21 CEST 2014


On May 14, 2014, at 4:43 PM, Andreas Leha wrote:

> Hi David,
> 
> David Winsemius <dwinsemius at comcast.net> writes:
> 
>> On May 14, 2014, at 3:44 PM, Andreas Leha wrote:
>> 
>>> Hi David,
>>> 
>>> Thank you for following up on that.
>>> 
>>> David Winsemius <dwinsemius at comcast.net> writes:
>>> 
>>>> On May 14, 2014, at 3:04 PM, 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...
>>>>> 
>>>> 
>>>> Have your read the help file for pdf()? The first argument is the default naming mechanism:
>>>> 
>>>> file = ifelse(onefile, "Rplots.pdf", "Rplot%03d.pdf")
>>>> 
>>>> So if you call pdf() then it will write to the working directory with a named constructed from those choices.
>>>> 
>>> 
>>> Sorry for being unclear.  I am aware of that.  My use case is, that
>>> apparently, I have called pdf("somefile.pdf") somewhen during a long
>>> lasting R session.
>>> 
>>> I tend to have separate R sessions open in parallel
>>> for the project I am working on.  So, I can easily have R session open
>>> that run for weeks.
>>> 
>>> So, when I return to an R session (when I got some additional data, I
>>> was asking for, e.g.) after several days, I do not know what that call
>>> was.  And for several reasons, the actual call (pdf("somefile.pdf"))
>>> might not even be visible in my R session.
>>> 
>>> In that situation, I would like to know, which is the open file for the
>>> current device.
>> 
>> Well, if you acheived success with the pdf() call by executing
>> dev.off() then you closed that device. The previous device does leave
>> behind a footprint but not the entire name of hte file or the
>> contents.
>> 
>> ?dev.prev
>> 
>>> old.dev <-dev.prev()
>>> str(old.dev)
>> Named int 5
>> - attr(*, "names")= chr "pdf"
>> 
>> 
>> If you had not changed working directory you may be able to then look
>> at all of the pdf files. You can get information with file,info()
>> 
>> ?file.info
>> 
>> 
> 
> Thanks for the follow-up.  I take it, there is no 'easy' way in R to
> achieve this.
> 
> The best solution (on unix systems) that I have come up with is this:
>  system(paste("lsof -p", Sys.getpid(), "| grep pdf"))
> (which obviously works only before dev.off())
> 
> I was hoping for some more reliable way to achieve this, but it'll work
> for me.

I guess my definition of easy is different than yours. I couldn't quite believe that you had left behind an open pdf device and expected things to be in the same state they had been several days later. I try to close my devices to get the output "solidified". Generating a new plot is not generally costly.

-- 

David Winsemius
Alameda, CA, USA



More information about the R-help mailing list