I'm not sure why the focal function I've been using in Raster does not work with Terra. This simplified example uses a custom function to apply the focal filter only to pixels with a value of 0 and it "grows" the pixels out to neighboring pixels (rooks case)....It works with raster, but I get the following message when attempting in Terra: Error: [Focal] test failed
library(raster)
tmpRas = raster(matrix(c(1:14,0,16:36,6,6))
plot(tmpRas)
grow0 = function(x){if (0%in%x){return(0)} else {return(x[5])}
w=matrix(c(NA,1,NA,1,1,1,NA,1,NA),3,3)
FocalRas = focal(tmpRas, w, fun = grow0, pad=TRUE, padValue = 1)
plot(FocalRas)
Any ideas on why this same method does not work with Terra, or is there an easier way to do what I'm trying to accomplish? Here is the same workflow using Terra:
library(terra)
tmpRast = rast(matrix(c(1:14,0,16:36),6,6)
FocalRast = focal(tmpRast, w, fun = grow0)