I'm working on a function for raster package. In the end of the function I have to use stack() to create a RasterStack. When I do this the data source is in memory. I want to have the data source on a file.

For example if I use crop() I can do:

asd <- crop(raster, extent, filename="something.tif")

I can't do the same with stack(), but I need to have the results on files, because it writes all files on RasterTmpFiles and I end having memory problems.

  • If you don't need the stack for computation you could write your process as a loop. e.g. loop over each raster where you: create/read | analyse | write result || repeat for next raster. – MikeRSpencer Aug 16 '15 at 21:10
  • I'm sorry but I need the stack for futhers calculation... – Guillermo Olmedo Aug 17 '15 at 14:01
  • Will you not continue to have memory problems if you have all the rasters open in a stack? Or do the memory problems happen when you have multiple stacks open? If so, perhaps you can work on one stack at a time. – MikeRSpencer Aug 17 '15 at 14:03
  • yeah, that's the problem. I have to work with different stacks, and every stack is a different data. I have to combine later those stacks, thats why I have to have all of them open at the same time. And, of course I have memory problems if I try to have all of them in memory. – Guillermo Olmedo Aug 17 '15 at 20:54
  • A solution could be to use a raster series in GRASS. You can use the spgrass6 package so your interface is still R, but you'll be handing off the heavy lifting to GRASS which is generally happier about it. – MikeRSpencer Aug 17 '15 at 20:56

I'm not sure I'm understanding the question properly but I believe you simply want to write a multispectral raster to disk. If, for example, you want a multilayer geotiff you just have to run the following line of code:

writeRaster(yourStackObject, filename="multilayer.tif", options="INTERLEAVE=BAND", overwrite=TRUE)

In filename you can include whichever path you want or if you only provide the filename the tif is written to your working directory.


If you want to load this stack back into R you just have to use the stack or brick functions:

mystack = stack("multilayer.tif")
mybrick = brick("multilayer.tif")

And you can obtain a band from these objects with the subset function, for example:

band1 = subset(mybrick,subset=1)

If temporary files are giving you problems you should clean them every now and then, for example right after writing your raster stacks you can use this code to delete all temporary raster files:

  • Sounds good. I have to add two more lines to the code, one for saving the raster to a file, and another to load the raster from the file. I was expecting to solve it with less code, like when I use crop. But it's a good approach. Thank you! – Guillermo Olmedo Aug 18 '15 at 15:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.