2

I am bringing in 10+ raster files (one-band files, not very big) into R from a folder outside of my working directory. Individually calling the files works, but it's getting tedious. I have seen people use code similar to this:

require(raster) 
current.list <- list.files(path="Y:/Spatial/RasterData/current", 
     pattern =".tif", full.names=TRUE)
c.stack<- stack(current.list)

However, it is raising various errors for me. Including, ".rasterObjectFromFile(x, band = band, objecttype = "RasterLayer", : Cannot create a RasterLayer object from this file. (file does not exist)"

Note, I've tried several different formats (.grd, .img) and often also require(rgdal) prior to running the command. Thanks in advance for your thoughts!

lorena
  • 25
  • 1
  • 5
  • Hi Lorena - please show what current.list returns. Perhaps it is an issue with the filenames. Also, you may want to see: http://stackoverflow.com/questions/6464235/problem-importing-usda-crop-data-layer-gtiff-in-r-using-raster-package – John Oct 08 '14 at 23:47
  • Thanks, @John (I read your question more carefully this time) current.list returns a list of my files including my .tifs and then auxiliary files e.g., [10] ...t_curr.tif" [11] "...t_curr.tif.aux.xml" [12] "...t_curr.tif.xml" So, I guess I should be asking, how do I override/eliminate these files which are needed in arcGIS? – lorena Oct 15 '14 at 16:33

2 Answers2

7

I haven't tested it, but I think if you add a "$" sign to the end of your pattern = ".tif" portion, i.e.:

current.list <- list.files(path="Y:/Spatial/RasterData/current", 
     pattern =".tif$", full.names=TRUE)

...things should work. The "$" symbol essentially means end of string, so you will only list files ending in ".tif" and nothing beyond (e.g. "tif.aux.xml").

Best of luck, and let me know if that doesn't do it for you.

John
  • 802
  • 2
  • 9
  • 19
0

I had this problem. I just had to set my working directory to be the same as where the rasters to import were and then it worked fine.

Jen
  • 1
  • But sometimes it might not be prudent/possible to change the wd (i.e. when you are accessing a data source you commonly use and don't want to reimport it to every WD you use). What would you do then? – Gmichael Mar 25 '20 at 12:12