I need to run calculations on large multi-band rasters and export a RasterBrick, and am trying to do so using the calc()
function in the raster package, for the purpose of memory efficiency. The function runs fine on its own, but when I try to include it in calc()
, I keep getting this error:
Error in .calcTest(x[1:5], fun, na.rm, forcefun, forceapply) :
cannot use this function
How can I make this work?
Simplified code:
fn = system.file("external/test.grd", package="raster")
s = stack(fn, fn, fn, fn)
out = calc(s, fun = function(x){
for (i in 1:nlayers(x)){
x[[i]] = x[[i]] - cellStats(x[[i]], "min")
x[[i]] = x[[i]]* 5
}
list = unstack(x)
out = brick(list)
return(out)
}
)
Error in .calcTest(x[1:5], fun, na.rm, forcefun, forceapply) :
cannot use this function