Removing temporary files created by pdf ()

When saving charts in pdf R format, a temporary file is created for each chart (for example, /tmp/RtmpFKQqjI/pdf317d27df81a0). After drawing many graphs in the pdf section my / tmp the memory runs out and R stops working (my desktop also freezes).

A small code example:

for (i in 1:10) {
    pdf(file=paste(i, ".pdf", sep=""))
    plot(1:10)
    dev.off()
}

list.files(path=tempdir(), pattern="^pdf.", full.names=TRUE)
# [1] "/tmp/RtmpFKQqjI/pdf317d27df81a0" "/tmp/RtmpFKQqjI/pdf317d28ed0612"
# [3] "/tmp/RtmpFKQqjI/pdf317d295c2453" "/tmp/RtmpFKQqjI/pdf317d304bb025"
# [5] "/tmp/RtmpFKQqjI/pdf317d3332d7fe" "/tmp/RtmpFKQqjI/pdf317d3921428f"
# [7] "/tmp/RtmpFKQqjI/pdf317d4cf812ca" "/tmp/RtmpFKQqjI/pdf317d5082bebe"
# [9] "/tmp/RtmpFKQqjI/pdf317d560d326"  "/tmp/RtmpFKQqjI/pdf317d674b25ea"

(Same results for pdf(file="Rplots%03d.pdf"); for (i in 1:10) { ... }; dev.off().)

Why doesn't R delete these temporary files after the call dev.off()?

As a workaround, I add the following line after each dev.off():

unlink(list.files(path=tempdir(), pattern="^pdf.", full.names=TRUE))

Is there a better way?

+5
source share
2 answers

I usually agree with @stark that this is a (minor) error in the implementation of the R device pdf.

, cairo_pdf, pdf , , temp.

for (i in 1:2) {
    cairo_pdf(file=paste(i, ".pdf", sep=""))
    plot(1:10)
    dev.off()
}
list.files(path=tempdir(), pattern="^pdf.", full.names=TRUE)
# character(0)
+4

dev.off, R pdf-. . , PDF tmp .

+3

All Articles