filename of current device

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

filename of current device

Andreas Leha
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...

Regards,
Andreas

______________________________________________
[hidden email] mailing list
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.
Reply | Threaded
Open this post in threaded view
|

Re: filename of current device

David Winsemius

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.


> Regards,
> Andreas
>
> ______________________________________________
> [hidden email] mailing list
> 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.

David Winsemius
Alameda, CA, USA

______________________________________________
[hidden email] mailing list
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.
Reply | Threaded
Open this post in threaded view
|

Re: filename of current device

Andreas Leha
Hi David,

Thank you for following up on that.

David Winsemius <[hidden email]> 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.

I hope that makes my question clearer.

Regards,
Andreas

______________________________________________
[hidden email] mailing list
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.
Reply | Threaded
Open this post in threaded view
|

Re: filename of current device

David Winsemius

On May 14, 2014, at 3:44 PM, Andreas Leha wrote:

> Hi David,
>
> Thank you for following up on that.
>
> David Winsemius <[hidden email]> 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


>
> I hope that makes my question clearer.
>
> Regards,
> Andreas
>
> ______________________________________________
> [hidden email] mailing list
> 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.

David Winsemius
Alameda, CA, USA

______________________________________________
[hidden email] mailing list
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.
Reply | Threaded
Open this post in threaded view
|

Re: filename of current device

Andreas Leha
Hi David,

David Winsemius <[hidden email]> writes:

> On May 14, 2014, at 3:44 PM, Andreas Leha wrote:
>
>> Hi David,
>>
>> Thank you for following up on that.
>>
>> David Winsemius <[hidden email]> 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.

Thanks again,
Andreas

______________________________________________
[hidden email] mailing list
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.
Reply | Threaded
Open this post in threaded view
|

Re: filename of current device

David Winsemius

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

> Hi David,
>
> David Winsemius <[hidden email]> writes:
>
>> On May 14, 2014, at 3:44 PM, Andreas Leha wrote:
>>
>>> Hi David,
>>>
>>> Thank you for following up on that.
>>>
>>> David Winsemius <[hidden email]> 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

______________________________________________
[hidden email] mailing list
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.
Reply | Threaded
Open this post in threaded view
|

Re: filename of current device

Jeff Newmiller
In reply to this post by Andreas Leha
You know, there is a more conventional way to handle this (at least from an R users perspective): Don't do that.

The normal method of analysis in R is to perform analysis computations in separate functions than the functions that display/print results. You can easily take this one step further and put your analysis functions in a separate R file than your display functions, and save the results (typically) to an RData file. That way your display operations can run at your convenience (interactively), and there is no concern that a pdf file sits there open for long periods.
---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<[hidden email]>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
---------------------------------------------------------------------------
Sent from my phone. Please excuse my brevity.

On May 14, 2014 4:43:31 PM PDT, Andreas Leha <[hidden email]> wrote:

>Hi David,
>
>David Winsemius <[hidden email]> writes:
>
>> On May 14, 2014, at 3:44 PM, Andreas Leha wrote:
>>
>>> Hi David,
>>>
>>> Thank you for following up on that.
>>>
>>> David Winsemius <[hidden email]> 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.
>
>Thanks again,
>Andreas
>
>______________________________________________
>[hidden email] mailing list
>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.

______________________________________________
[hidden email] mailing list
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.
Reply | Threaded
Open this post in threaded view
|

Re: filename of current device

Prof Brian Ripley
In reply to this post by Andreas Leha
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,                  [hidden email]
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

______________________________________________
[hidden email] mailing list
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.
Reply | Threaded
Open this post in threaded view
|

Re: filename of current device

Andreas Leha


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

______________________________________________
[hidden email] mailing list
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.