Commit 02c96ac5 authored by Cynthia Crowley's avatar Cynthia Crowley

Allow additional args to be passed to array_apply() and rsapply()

e.g. rsapply(rasterstack, AD_pval, distribution="gev", Nsim=1000, gof="AD")
parent 011128b4
Pipeline #78961488 passed with stages
in 19 minutes and 12 seconds
......@@ -17,15 +17,16 @@
#' @param fun a function to apply at each row and column of the input
#' array. The function will be called with a vector of the
#' z-values as a single argument.
#' @param ... additional arguments to be passed to \code{fun}
#' @return an array containing the values returned by \code{fun} at
#' each pixel
#' @export
array_apply <- function(arr, fun) {
array_apply <- function(arr, fun, ...) {
if (parallel_backend_exists()) {
apply_fn <- parallel::parApply
} else {
apply_fn <- apply
}
return(aperm(apply_fn(X=arr, MARGIN=c(2,1), FUN=fun)))
return(aperm(apply_fn(X=arr, MARGIN=c(2,1), FUN=fun, ...)))
}
......@@ -25,6 +25,7 @@
#' will be called with a vector representing the pixel values of
#' each raster in the stack, at a given (x,y).
#' @param names the names of the values returned by fun
#' @param ... additional arguments to be passed to \code{fun}
#' @return a new RasterLayer or RasterStack containing the values returned by
#' \code{fun} at each pixel.
#'@examples
......@@ -44,11 +45,11 @@
#'}, names=c('min', 'ave', 'max'))
#'}
#'@export
rsapply <- function(rast, fun, names=NULL) {
rsapply <- function(rast, fun, names=NULL, ...) {
# Copy our inputs to an in-memory matrix, for speed
data <- raster::as.array(rast)
result_data <- array_apply(data, fun)
result_data <- array_apply(data, fun, ...)
if (length(dim(result_data)) == 2) {
# fun returned a single value, so we return a raster
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment