From 1cf7293fa2282bd0b6dcb3f8632ee3b1dfc022e6 Mon Sep 17 00:00:00 2001 From: Victoria Agudetse Roures Date: Thu, 21 Dec 2023 14:36:28 +0100 Subject: [PATCH 1/2] Subset indices parameter as list --- R/CST_Subset.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/CST_Subset.R b/R/CST_Subset.R index 2e69c1f9..fa314329 100644 --- a/R/CST_Subset.R +++ b/R/CST_Subset.R @@ -87,7 +87,7 @@ CST_Subset <- function(x, along, indices, drop = FALSE, var_dim = NULL, # Adjust coordinates for (dimension in 1:length(along)) { dim_name <- along[dimension] - index <- indices[[dimension]] + index <- indices[dimension] # Only rename coordinates that have not been dropped if (dim_name %in% names(x$dims)) { # Subset coordinate by indices -- GitLab From a6ac6dbfeb4635fb8687c1f4041885dc950113d6 Mon Sep 17 00:00:00 2001 From: Eva Rifa Date: Thu, 21 Dec 2023 15:24:28 +0100 Subject: [PATCH 2/2] Add unit test for the correction and add any() inside .subset_with_attrs --- R/CST_Subset.R | 2 +- tests/testthat/test-CST_Subset.R | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/R/CST_Subset.R b/R/CST_Subset.R index fa314329..28c8498c 100644 --- a/R/CST_Subset.R +++ b/R/CST_Subset.R @@ -150,7 +150,7 @@ CST_Subset <- function(x, along, indices, drop = FALSE, var_dim = NULL, # Function to subset with attributes .subset_with_attrs <- function(x, ...) { args_subset <- list(...) - if (is.null(dim(x)) | length(dim(x)) == 1) { + if (any(is.null(dim(x)), length(dim(x)) == 1)) { l <- x[args_subset[['indices']][[1]]] } else { l <- ClimProjDiags::Subset(x, along = args_subset[['along']], diff --git a/tests/testthat/test-CST_Subset.R b/tests/testthat/test-CST_Subset.R index 9fc04b48..2a4fdd1f 100644 --- a/tests/testthat/test-CST_Subset.R +++ b/tests/testthat/test-CST_Subset.R @@ -50,6 +50,26 @@ test_that("2. Output checks: CST_Subset", { names(res1$coords), c("member", "ftime", "lon") ) + ## lat + expect_equal( + res1$coords$lat, + NULL + ) + ## lon + expect_equal( + as.vector(res1$coords$lon), + c(6, 7) + ) + ## sdate + expect_equal( + res1$coords$sdate, + NULL + ) + ## member + expect_equal( + as.vector(res1$coords$member), + c(1,2) + ) # Check attrs expect_equal( names(res1$attrs), -- GitLab