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!

2
  • Hi Lorena - please show what current.list returns. Perhaps it is an issue with the filenames. Also, you may want to see: stackoverflow.com/questions/6464235/…
    – John
    Oct 8, 2014 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, 2014 at 16:33

2 Answers 2

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.

0
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.

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, 2020 at 12:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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