Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/pkgdown.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ on:
push:
branches: [main, master]
pull_request:
branches: [ main ]
release:
types: [published]
workflow_dispatch:
Expand Down
33 changes: 33 additions & 0 deletions R/combineRDBESDataObjects.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()

Expand Down
37 changes: 37 additions & 0 deletions tests/testthat/test-combineRDBESDataObjects.R
Original file line number Diff line number Diff line change
Expand Up @@ -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