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?
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.
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.
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With