Remove ClimProjDiags and s2dv dependency
Hi @aho,
I open this issue in order to bring up some ideas to temporary remove Subset dependency from packages.
In this package there are 2 functions that use ClimProjDiags::Subset:
Here there are 3 examples with possible options to subset data in a similar way that Subset from ClimProjDiags does:
Options to subset data
# example array to subset
res <- array(1:12, dim = c(a = 2, b = 2, c = 3, d = 1))
dim_remove <- c('a', 'b')
- Add an auxiliary function that has some code lines from Subset
# Option 1: Add an auxiliary function that has some code lines from Subset
index_array <- function(x, dim, value, drop = FALSE) {
indices <- rep(list(bquote()), length(dim(x)))
dims <- which(names(dim(x)) %in% dim)
indices[dims] <- value
call <- as.call(c(list(as.name("["), quote(x)), indices, list(drop = drop)))
eval(call)
}
res1 <- index_array(res, dim = dim_remove, value = 1)
dim(res1)
# a b c d
# 1 1 3 1
- Use multiApply
# Option 2: multiApply
res2 <- array(1:12, dim = c(a = 2, b = 2, c = 3, d = 1))
dims <- dim(res2)
for (dimi in dim_remove) {
res2 <- Apply(res2, target_dims = dimi, fun = function(x) {
x <- x[1]
})$output1
}
dim(res2) <- c(dim(res2), rep(1, length(dim_remove)))
names(dim(res2)) <- c(names(dims)[-which(names(dims) %in% dim_remove)], dim_remove)
dim(res2)
# c d a b
# 3 1 1 1
- Use external function abind
# Option 3: abind
res <- array(1:12,dim=c(a = 2, b = 2, c = 3, d = 1),
dimnames=list(letters[1:2],LETTERS[1:2],letters[1:3], letters[1]))
library(abind)
res3 <- asub(res, list(1), c(1), drop=FALSE)
res3 <- asub(res3, list(1), c(2), drop=FALSE)
dim(res3)
# a b c d
# 1 1 3 1
Development
In branch: develop-remove_Subset !42 (merged) the sustitution has been made for Option 1. I have chosen this option because it doesn't require to load external packages and I think it's more efficient than the multiApply option. Also, there may be other options that I haven't think about it.
Best,
Eva