[R] graphics - current filename

Jan T. Kim jtk at cmp.uea.ac.uk
Mon Feb 14 18:12:23 CET 2005


On Mon, Feb 14, 2005 at 07:23:23AM -0800, Thomas Lumley wrote:
> On Mon, 14 Feb 2005, Uwe Ligges wrote:
> 
> >Paul Sorenson wrote:
> >
> >>I would like to query R for the current (or last used) filename for a 
> >>graphics device.
> >>
> >>Eg after png(filename="plot%02d.png") I would like something like the 
> >>output of dev.cur() but with the %02d expanded to the current name.
> >
> >You cannot, it is handled internally and the name is not returned.
> >So you have to workaround yourself either by specifying filenames yourself 
> >and looping over the png() calls, or counting yourself ...
> 
> Or just look on the disk with list.files().
> 
>   sort(list.files(pattern="plot[0-9][0-9]\\.png"),
>                   decreasing=TRUE)[1]

As a note of caution: Don't use this technique if you intend to run your
program more than once (unless you arrange for your program to remove
any preexisting plot[0-9][0-9].png files before entering into the loop in
question, which may also prove undesired some day down the line...).

Otherwise, if you e.g. repeat the same thing that produces files
plot01.png to plot99.png, the first run will work as intended, but the
second run will deal with plot99.png in each cycle of the loop...

The idea of querying the device for the current file name is basically
correct, because the device would be the authorive source of that
information, but as that isn't available, I'd second the recommendation
to specifying the file names yourself. Then, you are the authoritative
source.

Kind regards, Jan
-- 
 +- Jan T. Kim -------------------------------------------------------+
 |    *NEW*    email: jtk at cmp.uea.ac.uk                               |
 |    *NEW*    WWW:   http://www.cmp.uea.ac.uk/people/jtk             |
 *-----=<  hierarchical systems are for files, not for humans  >=-----*




More information about the R-help mailing list