Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing temporary files created by pdf()

Tags:

plot

r

pdf

When storing plots in a pdf R generates a temporary file (e.g. /tmp/RtmpFKQqjI/pdf317d27df81a0) for each plot. After drawing many plots into a pdf my /tmp partition runs out of memory and R stops working (my desktop also freezes).

Little 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 remove this temporary files after calling 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?

like image 452
sgibb Avatar asked Jul 15 '12 13:07

sgibb


People also ask

Where are PDF temporary files stored?

In the case of Adobe Reader, the most popular PDF application toolbox today, the temporary files are located in the following folder: "C:\Users\%UserName%\AppData\Roaming\Adobe\Acrobat\1X. 0\AutoSave\" . Unfortunately, this is a temporary folder that gets deleted when you close Adobe Reader.

Can you delete Adobe temp files?

1 Correct answer Hi, You can delete all the files in the AppData\Local folder by going into Windows Settings -> System -> Apps & features -> Adobe Experience Design CC (Beta) -> Advanced Options -> Press Reset.

Why can't I delete temporary Internet files?

Use Disk Cleanup If the Windows Settings menu refuses to delete temporary files, you can use the Windows Disk Cleanup utility; it analyzes and deletes temporary files just as better. Step 1: Launch the Windows Run box (Windows key + R), type control into the Open dialog box, and tap OK.


1 Answers

I tend to agree with @stark that this is a (minor) bug in R's pdf device implementation.

One workaround is to instead use the cairo_pdf device, which produces essentially identical pdfs but does not leave extra files lying around in the temp directory.

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)
like image 158
Josh O'Brien Avatar answered Oct 28 '22 20:10

Josh O'Brien