[R] How to test for open pdf file on Windows before calling cairo_pdf ?

Kevin Wright kw.stat at gmail.com
Thu Sep 11 22:06:17 CEST 2014


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]]



More information about the R-help mailing list