From f35abfb7a78c54f8907c72f2c35bde58e1e6874d Mon Sep 17 00:00:00 2001 From: aho Date: Fri, 14 Oct 2022 19:00:22 +0200 Subject: [PATCH 1/2] Change all warning() to .warning() --- R/ACC.R | 2 +- R/CDORemap.R | 2 +- R/Composite.R | 2 +- R/DiffCorr.R | 8 ++++---- R/EOF.R | 2 +- R/InsertDim.R | 4 ++-- R/PlotEquiMap.R | 2 +- R/REOF.R | 6 +++--- R/RPSS.R | 6 +++--- 9 files changed, 17 insertions(+), 17 deletions(-) diff --git a/R/ACC.R b/R/ACC.R index ceb1e94..64036f4 100644 --- a/R/ACC.R +++ b/R/ACC.R @@ -170,7 +170,7 @@ ACC <- function(exp, obs, dat_dim = 'dataset', lat_dim = 'lat', lon_dim = 'lon', } ## space_dim (deprecated) if (!missing("space_dim")) { - warning("Parameter 'space_dim' is deprecated. Use 'lat_dim' and 'lon_dim' instead.") + .warning("Parameter 'space_dim' is deprecated. Use 'lat_dim' and 'lon_dim' instead.") lat_dim <- space_dim[1] lon_dim <- space_dim[2] } diff --git a/R/CDORemap.R b/R/CDORemap.R index ce3ed35..f0044cb 100644 --- a/R/CDORemap.R +++ b/R/CDORemap.R @@ -632,7 +632,7 @@ CDORemap <- function(data_array = NULL, lons, lats, grid, method, cdo_version <- as.numeric_version( strsplit(suppressWarnings(system2("cdo", args = '-V', stderr = TRUE))[[1]], ' ')[[1]][5] ) - warning("CDORemap: Using CDO version ", cdo_version, ".") + .warning(paste0("CDORemap: Using CDO version ", cdo_version, ".")) if ((cdo_version >= as.numeric_version('1.7.0')) && (method == 'con')) { method <- 'ycon' } diff --git a/R/Composite.R b/R/Composite.R index be03ac9..03f0d58 100644 --- a/R/Composite.R +++ b/R/Composite.R @@ -170,7 +170,7 @@ Composite <- function(data, occ, time_dim = 'time', space_dim = c('lon', 'lat'), count_k <- plyr::count(occ) if (any(count_k$freq == 1)) { tmp <- count_k$x[which(count_k$freq == 1)] - warning(paste0("Composite K = ", tmp, " has length 1. The p-value is NA.")) + .warning(paste0("Composite K = ", tmp, " has length 1. The p-value is NA.")) } output_dims <- list(composite = c(space_dim, 'K'), diff --git a/R/DiffCorr.R b/R/DiffCorr.R index 95996b6..64e2743 100644 --- a/R/DiffCorr.R +++ b/R/DiffCorr.R @@ -135,9 +135,9 @@ DiffCorr <- function(exp, obs, ref, N.eff = NA, time_dim = 'sdate', stop('Parameter "method" must be "pearson" or "spearman".') } if (method == "spearman") { - warning(paste0("The test used in this function is built on Pearson method. ", - "To verify if Spearman method is reliable, you can run the ", - "Monte-Carlo simulations that are done in Siegert et al., 2017")) + .warning(paste0("The test used in this function is built on Pearson method. ", + "To verify if Spearman method is reliable, you can run the ", + "Monte-Carlo simulations that are done in Siegert et al., 2017")) } ## alpha if (!is.null(alpha)) { @@ -155,7 +155,7 @@ DiffCorr <- function(exp, obs, ref, N.eff = NA, time_dim = 'sdate', stop("Parameter 'test.type' must be 'two-sided' or 'one-sided'.") } #NOTE: warning can be removed in the next release - warning("The default significance test has changed after s2dv_1.2.0. The default method is 'two-sided'.") + .warning("The default significance test has changed after s2dv_1.2.0. The default method is 'two-sided'.") ## ncores if (!is.null(ncores)) { diff --git a/R/EOF.R b/R/EOF.R index d5c79a6..66e69da 100644 --- a/R/EOF.R +++ b/R/EOF.R @@ -131,7 +131,7 @@ EOF <- function(ano, lat, lon, time_dim = 'sdate', space_dim = c('lat', 'lon'), "length as the longitude dimension of 'ano'.")) } if (any(lon > 360 | lon < -360)) { - warning("Some 'lon' is out of the range [-360, 360].") + .warning("Some 'lon' is out of the range [-360, 360].") } ## neofs if (!is.numeric(neofs) | neofs %% 1 != 0 | neofs <= 0 | length(neofs) > 1) { diff --git a/R/InsertDim.R b/R/InsertDim.R index 36ce2f8..533683d 100644 --- a/R/InsertDim.R +++ b/R/InsertDim.R @@ -52,7 +52,7 @@ InsertDim <- function(data, posdim, lendim, name = NULL, ncores = NULL) { if (is.null(name)) { if (is.null(names(lendim))) { name <- 'new' - warning("The name of new dimension is not given. Set the name as 'new'.") + .warning("The name of new dimension is not given. Set the name as 'new'.") } else { name <- names(lendim) } @@ -63,7 +63,7 @@ InsertDim <- function(data, posdim, lendim, name = NULL, ncores = NULL) { } ## ncores if (!missing("ncores")) - warning("Argument 'ncores' is deprecated.") + .warning("Argument 'ncores' is deprecated.", tag = '! Deprecation: ') ############################### # Calculate InsertDim diff --git a/R/PlotEquiMap.R b/R/PlotEquiMap.R index c06f7bb..2c98430 100644 --- a/R/PlotEquiMap.R +++ b/R/PlotEquiMap.R @@ -929,7 +929,7 @@ PlotEquiMap <- function(var, lon, lat, varu = NULL, varv = NULL, dlon <- diff(lon) wher <- which(dlon > (mean(dlon) + 1)) if (length(wher) > 0) { - warning("Detect gap in 'lon' vector, which is considered as crossing the border.") + .warning("Detect gap in 'lon' vector, which is considered as crossing the border.") lon[(wher + 1):dims[1]] <- lon[(wher + 1):dims[1]] - 360 } lonb <- sort(lon, index.return = TRUE) diff --git a/R/REOF.R b/R/REOF.R index 2def222..c9c82cf 100644 --- a/R/REOF.R +++ b/R/REOF.R @@ -146,9 +146,9 @@ REOF <- function(ano, lat, lon, ntrunc = 15, time_dim = 'sdate', # ntrunc is bounded if (ntrunc != min(dim(ano)[time_dim], prod(dim(ano)[space_dim]), ntrunc)) { ntrunc <- min(dim(ano)[time_dim], prod(dim(ano)[space_dim]), ntrunc) - warning(paste0("Parameter 'ntrunc' is changed to ", ntrunc, ", the minimum among ", - "the length of time_dim, the production of the length of space_dim, ", - "and ntrunc.")) + .warning(paste0("Parameter 'ntrunc' is changed to ", ntrunc, ", the minimum among ", + "the length of time_dim, the production of the length of space_dim, ", + "and ntrunc.")) } # Area weighting is needed to compute the fraction of variance explained by diff --git a/R/RPSS.R b/R/RPSS.R index b5ff907..3d50d2b 100644 --- a/R/RPSS.R +++ b/R/RPSS.R @@ -200,9 +200,9 @@ RPSS <- function(exp, obs, ref = NULL, time_dim = 'sdate', memb_dim = 'member', } ## weights if (!is.null(weights)) { - warning(paste0("Parameter 'weights' is deprecated and will be removed in the next release. ", - "Use 'weights_exp' and 'weights_ref' instead. The value will be assigned ", - "to these two parameters now if they are NULL.")) + .warning(paste0("Parameter 'weights' is deprecated and will be removed in the next release. ", + "Use 'weights_exp' and 'weights_ref' instead. The value will be assigned ", + "to these two parameters now if they are NULL."), tag = '! Deprecation: ') if (is.null(weights_exp)) weights_exp <- weights if (is.null(weights_ref)) weights_ref <- weights } -- GitLab From 39932d420cb38d1af7a960ac44d0a83018f126b9 Mon Sep 17 00:00:00 2001 From: aho Date: Mon, 17 Oct 2022 09:52:45 +0200 Subject: [PATCH 2/2] Revise unit test for the warning --- tests/testthat/test-ACC.R | 2 +- tests/testthat/test-DiffCorr.R | 9 ++++----- tests/testthat/test-REOF.R | 7 +++---- 3 files changed, 8 insertions(+), 10 deletions(-) diff --git a/tests/testthat/test-ACC.R b/tests/testthat/test-ACC.R index 04e60ad..6431a9c 100644 --- a/tests/testthat/test-ACC.R +++ b/tests/testthat/test-ACC.R @@ -62,7 +62,7 @@ test_that("1. Input checks", { # space_dim (deprecated) expect_warning( ACC(exp1, obs1, space_dim = c('lat', 'lon'), lat = c(1, 2)), - "Parameter 'space_dim' is deprecated. Use 'lat_dim' and 'lon_dim' instead." + "! Warning: Parameter 'space_dim' is deprecated. Use 'lat_dim' and 'lon_dim'\n! instead." ) # lat_dim expect_error( diff --git a/tests/testthat/test-DiffCorr.R b/tests/testthat/test-DiffCorr.R index b8c8029..3498e8e 100644 --- a/tests/testthat/test-DiffCorr.R +++ b/tests/testthat/test-DiffCorr.R @@ -72,11 +72,10 @@ test_that("1. Input checks", { DiffCorr(exp1, obs1, ref1, method = 'asd', memb_dim = 'memb'), 'Parameter "method" must be "pearson" or "spearman".' ) - expect_warning( - DiffCorr(exp1, obs1, ref1, memb_dim = 'memb', method = 'spearman'), - paste0("The test used in this function is built on Pearson method. ", - "To verify if Spearman method is reliable, you can run the ", - "Monte-Carlo simulations that are done in Siegert et al., 2017") + tmp <- capture_warnings(DiffCorr(exp1, obs1, ref1, memb_dim = 'memb', method = 'spearman')) + expect_match( + tmp, + "! Warning: The test used in this function is built on Pearson method. To verify if\n! Spearman method is reliable, you can run the Monte-Carlo simulations\n! that are done in Siegert et al., 2017", all = F ) # alpha expect_error( diff --git a/tests/testthat/test-REOF.R b/tests/testthat/test-REOF.R index e118e1e..39747e2 100644 --- a/tests/testthat/test-REOF.R +++ b/tests/testthat/test-REOF.R @@ -92,10 +92,9 @@ test_that("1. Input checks", { ############################################## test_that("2. dat1", { - expect_warning( - REOF(dat1, lon = lon1, lat = lat1), - "Parameter 'ntrunc' is changed to 10, the minimum among the length of time_dim, the production of the length of space_dim, and ntrunc.", - fixed = TRUE + expect_equal( + suppressWarnings(REOF(dat1, lon = lon1, lat = lat1)), + REOF(dat1, lon = lon1, lat = lat1, ntrunc = 10) ) expect_equal( names(REOF(dat1, lon = lon1, lat = lat1, ntrunc = 10)), -- GitLab