I am attempting to write an R function, as part of a package, that takes a lidar tile (LAS object) and a vector of cut-points defining vertical layers, and returns a RasterStack where each layer has a layer point count as the cell value. The function makes use of the grid_metrics
function in the lidR
package. Here is a simplified version of the code to illustrate:
# Main package function
get_layer_counts <- function(las, breaks, res = 10) {
f <- function(Z) layer_count_metrics(Z, breaks)
stats <- lidR::grid_metrics(las, func = f(Z), res = res)
lidR:::as.raster.lasmetrics(stats)
}
# Metrics function to count points within height intervals
layer_count_metrics <- function(Z, breaks) {
zcat <- findInterval(Z, breaks, left.open = TRUE)
as.list(table(zcat))
}
Unfortunately this gives an error: "Error in eval(call) : object 'f' not found" which comes from the lidR:::lasaggregate
function.
It seems (I think) that unless the function passed to grid_metrics
is defined in the global environment it will not be found. Is that correct and, if so, can anyone suggest a work-around?