diff --git a/.github/workflows/pkgdown.yaml b/.github/workflows/pkgdown.yaml index bfc9f4d..3de2a83 100644 --- a/.github/workflows/pkgdown.yaml +++ b/.github/workflows/pkgdown.yaml @@ -4,6 +4,7 @@ on: push: branches: [main, master] pull_request: + branches: [ main ] release: types: [published] workflow_dispatch: diff --git a/R/combineRDBESDataObjects.R b/R/combineRDBESDataObjects.R index e58831f..865171e 100644 --- a/R/combineRDBESDataObjects.R +++ b/R/combineRDBESDataObjects.R @@ -9,6 +9,13 @@ #' @param strict (Optional) This function validates its input data - should #' the validation be strict? The default is TRUE. #' +#' @details +#' When combining RDBESDataObjects from different hierarchies (e.g., H1 and H5), +#' a warning is issued. The resulting combined object will have a mixed hierarchy, +#' which may be structurally and statistically invalid for some analyses. However, +#' such combinations can be useful for fisheries overviews, annual reports, or +#' countries performing broader estimations. +#' #' @return the combination of \code{RDBESDataObject1} and \code{RDBESDataObject2} #' @seealso \link[data.table]{rbindlist} #' @export @@ -31,6 +38,32 @@ combineRDBESDataObjects <- function(RDBESDataObject1, validateRDBESDataObject(RDBESDataObject1, verbose = verbose, strict = strict) validateRDBESDataObject(RDBESDataObject2, verbose = verbose, strict = strict) + + # Check for multiple hierarchies + hierarchy1 <- NULL + hierarchy2 <- NULL + + if (!is.null(RDBESDataObject1$DE) && nrow(RDBESDataObject1$DE) > 0) { + hierarchy1 <- unique(RDBESDataObject1$DE$DEhierarchy) + } + + if (!is.null(RDBESDataObject2$DE) && nrow(RDBESDataObject2$DE) > 0) { + hierarchy2 <- unique(RDBESDataObject2$DE$DEhierarchy) + } + + # Warn if combining different hierarchies + if (!is.null(hierarchy1) && !is.null(hierarchy2) && + length(hierarchy1) > 0 && length(hierarchy2) > 0) { + if (!all(hierarchy1 %in% hierarchy2) || !all(hierarchy2 %in% hierarchy1)) { + warning("Combining RDBESDataObjects from different hierarchies (", + paste(hierarchy1, collapse = ", "), " and ", + paste(hierarchy2, collapse = ", "), + "). This creates a mixed hierarchy object that may be structurally ", + "and statistically invalid for some analyses.", + call. = FALSE) + } + } + # Create an empty RDBESDataObject as the basis of what we will return myRDBESDataObject <- createRDBESDataObject() diff --git a/tests/testthat/test-combineRDBESDataObjects.R b/tests/testthat/test-combineRDBESDataObjects.R index 9242557..5cf3803 100644 --- a/tests/testthat/test-combineRDBESDataObjects.R +++ b/tests/testthat/test-combineRDBESDataObjects.R @@ -40,4 +40,41 @@ test_that("combineRDBESDataObjects returns valid RDBESDataObject when supplied expect_error(validateRDBESDataObject(myCombinedObject), NA) }) +test_that("combineRDBESDataObjects warns when combining objects from different hierarchies", { + + myObject1 <- importRDBESDataCSV(rdbesExtractPath = "./h1_v_20250211") + myObject2 <- importRDBESDataCSV(rdbesExtractPath = "./h5_v_20250211") + + # Check these are valid objects before we try and combine them + expect_error(validateRDBESDataObject(myObject1), NA) + expect_error(validateRDBESDataObject(myObject2), NA) + + # Expect a warning about different hierarchies + expect_warning(combineRDBESDataObjects(RDBESDataObject1=myObject1, + RDBESDataObject2=myObject2), + "Combining RDBESDataObjects from different hierarchies") +}) + +test_that("combineRDBESDataObjects does not warn when combining objects from same hierarchy", { + + myObject1 <- importRDBESDataCSV(rdbesExtractPath = "./h1_v_20250211") + myObject2 <- importRDBESDataCSV(rdbesExtractPath = "./h1_v_20250211") + + # Expect no warning about different hierarchies (but will have duplicate rows error later) + expect_warning(combineRDBESDataObjects(RDBESDataObject1=myObject1, + RDBESDataObject2=myObject2), + NA) +}) + +test_that("combineRDBESDataObjects does not warn when one object has no DE table", { + + myObject1 <- importRDBESDataCSV(rdbesExtractPath = "./h1_v_20250211") + myObject2 <- createRDBESDataObject() # Empty object with no DE + + # Expect no warning because one object has no hierarchy + expect_warning(combineRDBESDataObjects(RDBESDataObject1=myObject1, + RDBESDataObject2=myObject2), + NA) +}) + }) ## end capture.output