Skip to content

Commit

Permalink
add test_epiparameter function, relates #364
Browse files Browse the repository at this point in the history
  • Loading branch information
joshwlambert committed Aug 12, 2024
1 parent cdf7356 commit abc3ee6
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export(is_parameterised)
export(is_parameterized)
export(is_truncated)
export(parameter_tbl)
export(test_epiparameter)
importFrom(distributional,cdf)
importFrom(distributional,generate)
importFrom(lifecycle,deprecated)
Expand Down
34 changes: 33 additions & 1 deletion R/epiparameter.R
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ assert_epiparameter <- function(x) {
"summary_stats", "citation", "metadata", "method_assess", "notes"
)
missing_list_names <- list_names[!list_names %in% attributes(x)$names]
if (length(missing_list_names > 0)) {
if (length(missing_list_names) != 0) {
stop(
"Object is missing ", toString(missing_list_names), call. = FALSE
)
Expand Down Expand Up @@ -369,6 +369,38 @@ assert_epiparameter <- function(x) {
invisible(x)
}

#' Test whether an object is a valid `<epiparameter>` object
#'
#' @param x An \R object.
#'
#' @return A boolean `logical` whether the object is a valid `<epiparameter>`
#' object.
#' @export
test_epiparameter <- function(x) { # nolint cyclocomp_linter
if (!is_epiparameter(x)) return(FALSE)

list_names <- c(
"disease", "pathogen", "epi_dist", "prob_dist", "uncertainty",
"summary_stats", "citation", "metadata", "method_assess", "notes"
)
missing_list_names <- list_names[!list_names %in% attributes(x)$names]
if (length(missing_list_names) != 0) return(FALSE)

valid_elements <- checkmate::test_string(x$disease) &&
checkmate::test_string(x$epi_dist) &&
(checkmate::test_multi_class(
x$prob_dist, classes = c("distribution", "distcrete")
) || checkmate::test_string(x$prob_dist, na.ok = TRUE)) &&
all(
is.list(x$uncertainty), is.list(x$summary_stats), is.list(x$metadata)
) &&
inherits(x$citation, "bibentry") &&
checkmate::test_string(x$notes)

if (!valid_elements) return(FALSE)
return(TRUE)
}

#' Print method for `<epiparameter>` class
#'
#' @param x An `<epiparameter>` object.
Expand Down
18 changes: 18 additions & 0 deletions man/test_epiparameter.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit abc3ee6

Please sign in to comment.