Temporary files pile-up while using the `raster` package in R

Update: I’m not sure that this method has ever actually worked for me. I would love to hear success/failure for others. Restarting the computer seems to always free things up.

The raster package in R is incredibly useful and powerful free and open source solution for helping do geospatial analysis, especially if you are familiar with R, but don’t work regularly with another GIS software. It is also very useful even if you do, after all, you may not always be working somewhere that can afford licenses for commercial desktop GIS software (ahem, ArcGIS). Though I and my fellow students here at NC State have ready access to commercial software at no cost to ourselves, we really like learning to use and integrate R into our work, because it can then be reproduced and we can collaborate more easily. The quoted information in this post can be found here on Inside-R, a super-helpful reference site. The raster package was developed for:

“Reading, writing, manipulating, analyzing and modeling of gridded spatial data. The package implements basic and high-level functions. Processing of very large files is supported.

It’s that last part that leads to the pile-up of temporary files.

Another student and I are working on a project (it’s his thesis, so really he is doing the work) that is comparing outcomes of species distribution models using predictor variables at different resolutions across the entire extent of Oregon and California. This means processing through a lot of mapped surfaces during the fitting and prediction phases. The raster package is the only way to handle this, because the

“Functions in the raster package create temporary files if the values of an output RasterLayer cannot be stored in memory (RAM). This can happen when no filename is provided to a function and in functions where you cannot provide a filename (e.g. when using ‘raster algebra’).”

Now, in a “normal” R session (using the command line or GUI that comes with R installs) these temporary files are automatically removed at the start of each session. However, it seems that if you are using RStudio under certain settings (maybe the defaults, not positive on that part), then the temporary files may be retained even when you start a new session.  So, if you find that your hard drive is filling up and wondering “Where, why, and how do I fix it?” the solution is right there as part of the raster package with the removeTmpFiles function, which can be implemented to remove all the temporary files with the minimum age of the files indicated by the value for h measured in hours.

#Remove all temporary files that are more than 24 hours old:
removeTmpFiles(h=24)
#Remove all temporary files currently in existence:
removeTmpFiles(h=0)
Advertisements