Skip to content

Managing temporary files created when using the Raster package

4 messages · Julie Lee-Yaw, Jonathan Greenberg

#
Julie:

Why not just build a ?file.remove into your script?  Also, most raster
functions allow you to explicitly set a filename (see e.g. ?calc -->
the filename= parameter), rather than letting it generate a random
filename -- this would likely make it easier for you to figure out the
appropriate file to delete.

--j
On Sun, Mar 23, 2014 at 4:44 PM, Julie Lee-Yaw <julleeyaw at yahoo.ca> wrote:

  
    
6 days later
#
Hi Julie:

Glad it worked out!  Just for the sake of preserving this info on the
interwebs and to add some "tweaks" to your solutions, first, raster's
temporary files are stored in:

rasterOptions()$tmpdir

This folder can be changed to:

rasterOptions(tmpdir="/pathto/new/temp/dir")

Re: list files, if you want to full path, I use:
normalizePath(list.files(pattern=glob2rx("temp*"),full.names =TRUE))

full.names=TRUE will give you the full path to the file, although it
will be a relative path -- normalizePath "fixes" this to the absolute
path.  The pattern parameter for list.files is a "regular expression"
(http://en.wikipedia.org/wiki/Regular_expression), which many people
aren't terribly familiar with -- if you are more familiar with the
wildcard approach (your example suggests you are -- I am too), to be
safe, wrap that in a glob2rx() function so it it created properly:

glob2rx("temp*")

The pattern="temp*", since it is interpreted as a regular expression,
is not interpreted the way you probably thought it was, and when you
remove files you should be careful about that.  You could end up
deleting different files than you thought.  "*" in regular expressions
means something different than it does in wildcard patterns.

Cheers!

--j
On Sun, Mar 30, 2014 at 10:59 AM, Julie Lee-Yaw <julleeyaw at yahoo.ca> wrote: