From 9a25e63497823faec536a858c29cd7970238118b Mon Sep 17 00:00:00 2001 From: vagudets Date: Fri, 28 Mar 2025 09:31:29 +0100 Subject: [PATCH 1/2] Modify calls to Apply() to avoid using 'builtin' type functions, issue #54 --- R/MergeRefToExp.R | 9 ++++++--- R/PeriodAccumulation.R | 6 ++++-- R/PeriodMax.R | 3 +-- R/PeriodMin.R | 3 ++- R/SelectPeriodOnData.R | 2 +- R/SelectPeriodOnDates.R | 2 +- 6 files changed, 15 insertions(+), 10 deletions(-) diff --git a/R/MergeRefToExp.R b/R/MergeRefToExp.R index 6d9fd6e..69f722b 100644 --- a/R/MergeRefToExp.R +++ b/R/MergeRefToExp.R @@ -184,8 +184,10 @@ CST_MergeRefToExp <- function(data1, data2, start1 = NULL, end1 = NULL, names(dim(dates2)) <- time_dim } } - res <- Apply(list(dates1, dates2), target_dims = time_dim, - 'c', output_dims = time_dim, ncores = ncores)$output1 + res <- Apply(list(dates1, dates2), + target_dims = time_dim, + fun = function(x, ...) {c(x, ...)}, + output_dims = time_dim, ncores = ncores)$output1 if (inherits(dates1, 'Date')) { data1$attrs$Dates <- as.Date(res, origin = '1970-01-01') @@ -421,7 +423,8 @@ MergeRefToExp <- function(data1, data2, dates1 = NULL, dates2 = NULL, } } - data1 <- Apply(list(data1, data2), target_dims = time_dim, fun = 'c', + data1 <- Apply(list(data1, data2), target_dims = time_dim, + fun = function(x, ...) {c(x, ...)}, output_dims = time_dim, ncores = ncores)$output1 if (all(names(dim(data1)) %in% data1dims)) { diff --git a/R/PeriodAccumulation.R b/R/PeriodAccumulation.R index 8b0129f..a94d6f7 100644 --- a/R/PeriodAccumulation.R +++ b/R/PeriodAccumulation.R @@ -273,7 +273,7 @@ PeriodAccumulation <- function(data, dates = NULL, start = NULL, end = NULL, if (is.null(rollwidth)) { # period accumulation - total <- Apply(list(data), target_dims = time_dim, fun = sum, + total <- Apply(list(data), target_dims = time_dim, fun = function(...) {sum(...)}, na.rm = na.rm, ncores = ncores)$output1 } else { # rolling accumulation @@ -337,7 +337,9 @@ PeriodAccumulation <- function(data, dates = NULL, start = NULL, end = NULL, } } - data_accum <- rollapply(data = data_vector, width = rollwidth, FUN = sum, na.rm = na.rm) + data_accum <- rollapply(data = data_vector, width = rollwidth, + FUN = function(...) {sum(...)}, + na.rm = na.rm) if (!forwardroll) { data_accum <- c(rep(NA, rollwidth-1), data_accum) } else { diff --git a/R/PeriodMax.R b/R/PeriodMax.R index f038aff..ecb3376 100644 --- a/R/PeriodMax.R +++ b/R/PeriodMax.R @@ -205,11 +205,10 @@ PeriodMax <- function(data, dates = NULL, start = NULL, end = NULL, } } } - total <- Apply(list(data), target_dims = time_dim, fun = max, + total <- Apply(list(data), target_dims = time_dim, fun = function(...) {max(...)}, na.rm = na.rm, ncores = ncores)$output1 dim(total) <- c(dim(total), setNames(1, time_dim)) return(total) } - diff --git a/R/PeriodMin.R b/R/PeriodMin.R index 91f1cd2..e4d0c64 100644 --- a/R/PeriodMin.R +++ b/R/PeriodMin.R @@ -205,7 +205,8 @@ PeriodMin <- function(data, dates = NULL, start = NULL, end = NULL, } } } - total <- Apply(list(data), target_dims = time_dim, fun = min, + total <- Apply(list(data), target_dims = time_dim, + fun = function(...) {min(...)}, na.rm = na.rm, ncores = ncores)$output1 dim(total) <- c(dim(total), setNames(1, time_dim)) return(total) diff --git a/R/SelectPeriodOnData.R b/R/SelectPeriodOnData.R index 1cc2792..731abb5 100644 --- a/R/SelectPeriodOnData.R +++ b/R/SelectPeriodOnData.R @@ -121,7 +121,7 @@ SelectPeriodOnData <- function(data, dates, start, end, ncores = ncores)$output1 # when 29Feb is included the length of the output changes: regular <- Apply(list(res), target_dims = time_dim, - fun = sum, ncores = ncores)$output1 + fun = function(...) {sum(...)}, ncores = ncores)$output1 dims <- dim(data) dims[names(dims) == time_dim] <- max(regular) if (any(regular != max(regular))) { diff --git a/R/SelectPeriodOnDates.R b/R/SelectPeriodOnDates.R index 550aad9..b95c509 100644 --- a/R/SelectPeriodOnDates.R +++ b/R/SelectPeriodOnDates.R @@ -53,7 +53,7 @@ SelectPeriodOnDates <- function(dates, start, end, } # when 29Feb is included the length of the output changes: regular <- Apply(list(res), target_dims = time_dim, - fun = sum, ncores = ncores)$output1 + fun = function(...) {sum(...)}, ncores = ncores)$output1 dims <- dim(dates) dims[names(dims) == time_dim] <- max(regular) if (any(regular != max(regular))) { -- GitLab From 840fe0f479f0440d71173ee6e87cdf74a2fd95fd Mon Sep 17 00:00:00 2001 From: vagudets Date: Fri, 28 Mar 2025 09:58:06 +0100 Subject: [PATCH 2/2] Update NEWS.md --- NEWS.md | 1 + 1 file changed, 1 insertion(+) diff --git a/NEWS.md b/NEWS.md index f83b34c..025519f 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,6 +1,7 @@ # CSIndicators 1.1.2 (Release date: 2025-03-27) ### Fixes +- Avoid using 'builtin' type functions in call to multiApply::Apply() - CST_PeriodStandardization: Set na.rm = TRUE when replacing infinite values with maximum/minimum period values - CST_PeriodPET(): Add longname and other variable metadata - Not drop singleton time dimensions in -- GitLab