Apply() allows for the input data
to be a named list, but the names are not used when applying the user-defined function fun
. Instead, Apply() expects the order of the elements in data
to match the order of the arguments in fun
:
#'@param data One or a list of vectors, matrices or arrays. They must be in the
#' same order as expected by the function provided in the parameter 'fun'. The
#' dimensions do not necessarily have to be ordered. If the 'target_dims'
#' require a different order than the provided, \code{Apply} will automatically
#' reorder the dimensions as needed.
In this MR, if fun
has arguments and data
is a named list, data
is reordered inside Apply() to match the order of the arguments in fun
. If there are both named and unnamed elements, the unnamed elements will be placed at the end, in the same original order. This can be useful for example in the case where fun
has the ...
parameter, as in the following example:
library(multiApply)
source("R/Apply.R")
source("R/zzz.R")
library(abind)
library(plyr)
library(ClimProjDiags)
library(s2dv)
obs <- rnorm(10)
hcst1 <- rnorm(100)
hcst2 <- rnorm(100)
dim(obs) <- c(time = 1, syear = 10, ensemble = 1)
dim(hcst1) <- c(time = 1, syear = 10, ensemble = 10)
dim(hcst2) <- c(time = 1, syear = 10, ensemble = 10)
hcst <- list(hcst1, hcst2)
datos <- append(list(obs = obs), hcst)
crps <- Apply(datos, target_dims = c('syear', 'ensemble'),
fun = function(obs, ...) {
res <- abind(..., along = 2)
names(dim(res)) <- names(dim(obs))
obs <- Subset(obs, along = 'ensemble',
indices = 1, drop = 'selected')
mean(s2dv:::.CRPS(exp = res, obs = obs, dat_dim = NULL,
time_dim = 'syear',
memb_dim = 'ensemble'))},
ncores = 4)$output1
datos_notordered <- append(hcst, list(obs = obs))
crps_notordered <- Apply(datos_notordered, target_dims = c('syear', 'ensemble'),
fun = function(obs, ...) {
res <- abind(..., along = 2)
names(dim(res)) <- names(dim(obs))
obs <- Subset(obs, along = 'ensemble',
indices = 1, drop = 'selected')
mean(s2dv:::.CRPS(exp = res, obs = obs, dat_dim = NULL,
time_dim = 'syear',
memb_dim = 'ensemble'))},
ncores = 4)$output1
identical(crps, crps_notordered)
TODO:
-
Review sanity checks, add if necessary -
Apply reorder to target_dims -> Not necessary, as target_dims names are already mapped to fun argument names