I have a large number of rasters in grid format ("asc"). I created a list of files:

files <- list.files("D:/......../EnvironData", pattern='asc',
                    full.names = TRUE)

Then I want to stack all files:

dataStack <- raster::stack(files)

Unfortunately, I get the following error:

Error in .local(.Object, ...) :

Error in .rasterObjectFromFile(x, band = band, objecttype = "RasterLayer", : Cannot create a RasterLayer object from this file.

All raster files have the same spatial dimension and extent.

  • 1
    Likely one of your files is invalid somehow. Try lapply(seg_along(files), function(x) {print(x); raster(files[x])}) - the last number printed will be the first bad file (or just the last file).
    – mdsumner
    Jan 15, 2017 at 15:07
  • @mdsumner Thanks for suggestion. I did not find problem in my files. All are perfect.
    – NoviceStat
    Jan 15, 2017 at 16:02
  • Can you insert the output of the lapply call suggested by mdsummer into your question?
    – pat-s
    Jan 15, 2017 at 16:05
  • 1
    @pat-s I have 54 rasters, I tried if any of two rasters get stacked. Unfortunately none. variables
    – NoviceStat
    Jan 15, 2017 at 16:26
  • 1
    raster has some dusty corners, it's expected for something so general. You could use rgdal::readGDAL to read them individually then stack those, and there's another reader for this format in maptools, or maybe sp. The more you can explore and report specifics about the easier to debug.
    – mdsumner
    Jan 15, 2017 at 21:29

1 Answer 1


This problem happened to me too, but it was because my directory was large. R was unable to find the way, so he gave this answer: Error in .rasterObjectFromFile (x, band = band, objecttype = "RasterLayer",: Cannot create a RasterLayer object from this file. (File does not exist)

After I put it in another folder with a smaller path, it worked.

Your Answer

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

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