2

I have a RasterBrick and I am trying to add new layers to it, (with addLayer) but every time I try I receive the following message:

Error in sapply(x, fromDisk) & sapply(x, inMemory) : operations are possible only for numeric, logical or complex types

The brick and the layers have the same crs, res, and extent, and I reprojected the brick to match the layers.

Is there anything else that might be keeping the two from merging?

The only thing I can think of is that the brick is stored on disk and the layer is stored on memory- but would that actually effect anything?

  • 2
    Hard to tell without your data or your code. If you use readAll on the stack to bring it into memory (use inMemory to check) does it work? – Spacedman Apr 12 at 14:45
  • My code is just "x <- addLayer(y)". My data is the worldclim data and the elevation data (for the US) from getData (among others). I changed the extent, res and crs of worldclim to match the elevation data. I read the brick into memory and that's not the problem – birdoptera Apr 12 at 18:26
  • You're not adding anything to y. Are you trying to do x = addLayer(x, y)? See how important it is to show your code? – Spacedman Apr 12 at 18:42
  • And now I'm embarrassed about how dumb I am. That was exactly the problem. Thanks. – birdoptera Apr 12 at 18:48
  • Nah, its a badly named function! addLayers might be better, or it might not even be needed - you can do stack(a,b,c) and get the same effect I think... – Spacedman Apr 12 at 19:41
2

The addLayer function works by stacking all its arguments - to add a layer to an existing raster or stack make it the first argument. This is shown in the help:

 file <- system.file("external/test.grd", package="raster")
 s <- stack(file, file, file)
 r <- raster(file)
 s <- addLayer(s, r/2, r*2)

Running with only one argument produces that error:

> s = addLayer(s)
Error in sapply(x, fromDisk) & sapply(x, inMemory) : 
  operations are possible only for numeric, logical or complex types

Its not a particularly infomative error message.

addLayer appears to be equivalent to stack:

> s3 = stack(s,s,s)
> as3 = addLayer(s,s,s)

except in the case of adding nothing! stack(s) === s in this case!

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.