Riitters et al. (2020) propose a classification scheme for forest fragmentation (which can be applied to any habitat type). The scheme relies on calculating density (e.g., number of forested cells in a window around a focal cell) and connectivity (number of cases where neighboring cells are both forested). This function calculates these classes from a GRaster or SpatRaster in which the focal habitat type has cell values of 1, and non-focal habitat type has cell values of 0 or NA.
Note that by default, the SpatRaster and GRaster versions will create different results around the border of the raster. The SpatRaster version uses the terra::focal() function, which will not return an NA value when its window overlaps the raster border if the na.rm argument is TRUE. However, the GRaster version uses the GRASS tool r.neighbors, which does return NA values in these cases.
The fragmentation classes are:
Value provided by none: None (i.e., no forest; default is NA).
1: Patch
2: Transitional
3: Perforated
4: Edge
5: Undetermined (not possible to obtain when w = 3)
6: Interior