Hi Jonathan,

Thanks for this suggestion. It took me awhile to find all the places where temporary rasters were being generated. After finding them, I used filename in all commands that were generating the files, pointing them all to a temporary directory and calling them all "temp_" something. I then used the following lines of code to remove them at the end of each loop (note file.remove seemed to require me to be in the directory with the files to be removed...)

path<-"./myPermanentDirectory" # inside this directory, I had created a temp directory to dump temp files in
setwd(paste(path,"/tmpdir",sep="")) # change directory to where the temporary files are
do.call(file.remove,list(list.files(pattern="temp*)))
setwd(path) # go back to permanent directory

Thanks!

Julie




________________________________
 From: Jonathan Greenberg <jgrn@illinois.edu>

Cc: "r-sig-geo@r-project.org" <r-sig-geo@r-project.org> 
Sent: Sunday, March 23, 2014 11:58:50 PM
Subject: Re: [R-sig-Geo] Managing temporary files created when using the Raster package


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



> Hi
>
> I am doing a lot of raster calculations repeatedly (in a loop of sorts). As might be expected, this process is generating a lot of temporary files and filing up my hard drive before my script finishes. The first step in my script is to stack a set of very large rasters. This stack is then the input for the function that I'm using (e.g. the loop). Can I add in a line in my loop (e.g. removeTmpFiles) to delete temporary files created during the loop without ruining the integrity of the raster stack?
>
> For example:
>
> files<-list.files(pattern=".tif")
> myStack<-stack(files)
>
> myFunction<-function(x, rasterStack, y, z){
> # several calculations using the raster stack and x, y, z
> # non-raster results from the calculations get saved to file using write.csv()
> removeTmpFiles(h=0) # remove temp. raster files from disk???
> }
>
> lapply(x=list_of_x_values, myFunction, rasterStack=myStack, y=something, z=somethingelse)
>
> Is the removeTmpFiles line appropriate here?
>
[[elided Yahoo spam]]
>         [[alternative HTML version deleted]]
>
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo@r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>



-- 
Jonathan A. Greenberg, PhD
Assistant Professor
Global Environmental Analysis and Remote Sensing (GEARS) Laboratory
Department of Geography and Geographic Information Science
University of Illinois at Urbana-Champaign
259 Computing Applications Building, MC-150
605 East Springfield Avenue
Champaign, IL  61820-6371
Phone: 217-300-1924
http://www.geog.illinois.edu/~jgrn/
AIM: jgrn307, MSN: jgrn307@hotmail.com, Gchat: jgrn307, Skype: jgrn3007
	[[alternative HTML version deleted]]

