[R] How to test for open pdf file on Windows before calling cairo_pdf ?
Kevin Wright
kw.stat at gmail.com
Fri Sep 12 20:06:24 CEST 2014
What I finally ended up doing was using the file() command to see if I
could open a writeable connection to a pdf, then closing the connection
before opening the file with cairo_pdf.
However, my whole effort may be pointless. The cairo_pdf() device is
crashing R on Windows so often as to be mostly useless.
Kevin
On Thu, Sep 11, 2014 at 3:56 PM, Henrik Bengtsson <hb at biostat.ucsf.edu>
wrote:
> A poor mans solution would be to try to temporarily delete or move the
> file, depending exactly what you wish to do. You can also try:
>
> library("R.utils")
> pathname <- Arguments$getWritablePathname("file2.pdf", mustNotExist=FALSE)
>
> which does lots of assertions of write permissions of new and existing
> file, in the target directory etc. and tries to give an informative
> error if it fails. However, it does not have a particular
> test/message for "locked" PDF files. This is the error message I get
> when I view a PDF in Adobe Reader on Windows:
>
> > pathname <- Arguments$getWritablePathname("file2.pdf",
> mustNotExist=FALSE)
> [2014-09-11 13:42:14] Exception: No permission to modify existing file:
> file2.pd
> f
>
> at #03. getWritablePathname.Arguments(static, ...)
> - getWritablePathname.Arguments() is in environment 'R.utils'
>
> at #02. getWritablePathname(static, ...)
> - getWritablePathname() is in environment 'R.utils'
> - originating from '<text>'
>
> at #01. Arguments$getWritablePathname("file2.pdf", mustNotExist = FALSE)
> - Arguments$getWritablePathname() is local of the calling
> function
>
> Error: No permission to modify existing file: file2.pdf
> In addition: Warning message:
> In fileAccess.default(pathname, mode = 2) :
> file.access(..., mode=2) and file(..., open="ab") gives different
> results (0 != -1). Will use the file() results: file2.pdf
>
>
> Note that it is not all PDF viewers that lock/prevent files from being
> overwritten. For instance, when I view the same file using Foxit
> Reader, I don't get an error, e.g.
>
> library("R.utils")
> pathname <- Arguments$getWritablePathname("file2.pdf", mustNotExist=FALSE)
> pdf(pathname)
> plot(1:10)
> dev.off()
>
>
> Better yet, just use the R.devices package
> [http://cran.r-project.org/web/packages/R.devices/index.html] and
> it'll all be take care of for you:
>
> library("R.devices")
> devEval("png", name="file1", {
> plot(1:10)
> })
>
> library("R.devices")
> devEval("cairo_pdf", name="file2", {
> plot(1:10)
> }, ext="pdf")
>
> You'll get the above error message if, say, Adobe Reader is preventing
> the file from being overwritten. This approach will make sure to
> close any opened graphics devices, not to leave incomplete image files
> behind if there is an errors and so on. There are plenty of option
> for it, e.g. the default output directory is "./figures/", which can
> be changed. (The need for argument ext="pdf" is due to a minor bug
> that will be fixed in the next release.)
>
>
> Hope this helps,
>
> Henrik
> (author of R.utils and R.devices)
>
> On Thu, Sep 11, 2014 at 1:06 PM, Kevin Wright <kw.stat at gmail.com> wrote:
> > On Windows:
> >
> > The pdf("file1.pdf") command will throw an error if the file1.pdf is open
> > in a viewer. For example:
> >
> > pdf("file1.pdf")
> > plot(1:10)
> > dev.off()
> > shell.exec("file1.pdf")
> > pdf("file1.pdf") # Causes an error
> >
> > As suggested by the help page for file.access(), I normally use
> > try(pdf("file1.pdf")) to test if file1.pdf is open.
> >
> > Oddly, I cannot do the same using cairo_pdf. For example:
> >
> > cairo_pdf("file2.pdf")
> > plot(1:10)
> > dev.off()
> > shell.exec("file2.pdf")
> > cairo_pdf("file2.pdf") # No error
> >
> > It is not until a plotting command is called that an error is generated.
> >
> > Is there a way to test if a pdf file can be written to by cairo_pdf ?
> >
> > I'd rather not wrap all plotting commands with try()....I have many such
> > commands in my script and the choice of which one is first depends on
> user
> > input.
> >
> > Kevin Wright
> >
> > [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > R-help at r-project.org 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.
>
--
Kevin Wright
[[alternative HTML version deleted]]
More information about the R-help
mailing list