# example to be run if terra is present
if(requireNamespace("terra", quietly=TRUE)){
# randomly generated points
x <- rpsphere(100, output="polar")
# bandwidth grid
gr <- hexagrid(deg=13)
# output structure
out <- terra::rast(res=5)
# Manual example - for understanding what FUN is doing
cell<- locate(gr, x)
yNew <- cbind(as.data.frame(x), cell=cell)
# this is the default function (here named CellCount)
CellCount <- function(x) table(x$cell)
counts <- CellCount(yNew)
# create facelayer
fl <- facelayer(gr, counts)
# and resample
oneOut <- icosa::resample(fl, out)
terra::plot(oneOut, main="Density with default grid orientation")
points(x, pch=3, col="red")
# for density estimation
o <- grapply(x=x, out=out,y=gr, iter=7, FUN=CellCount, miss=0)
# visualize results
terra::plot(o)
points(x, pch=3, col="red")
}
Run the code above in your browser using DataLab