There's no mention of what happens with layer names in the documentation for
calc so I suspect the answer is "just because". If you rely on layer names in your code then you should probably explicitly set them any time you think they might change.
Note that arithmetic can change layer names - even though both operands here have the same name, the output is different:
Even simple functions of one raster can change layer names:
I think the best advice is to treat layer names as fragile and reset them when you need them, copying them from source rasters at the start of a processing step.
I'm not sure there's always a sensible default when doing arithmetic - suppose you are doing operations on more than one raster, which one do you use? Simpler to let the user decide. The
setNames function comes in very handy here:
Two rasters with differing names:
When multiplied, returns neither:
setNames and get a raster:
> rq = setNames(r*q, names(q))
> rq2 = setNames(r*q, names(r))
With whichever names you ask for:
a = setNames(b, n) is essentially the same as
a = b; names(a)=n; return(a)