From abc3ee610b16bb90e96baa56f486f4e123bc2e69 Mon Sep 17 00:00:00 2001 From: Joshua Lambert Date: Mon, 12 Aug 2024 16:55:02 +0100 Subject: [PATCH] add test_epiparameter function, relates #364 --- NAMESPACE | 1 + R/epiparameter.R | 34 +++++++++++++++++++++++++++++++++- man/test_epiparameter.Rd | 18 ++++++++++++++++++ 3 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 man/test_epiparameter.Rd diff --git a/NAMESPACE b/NAMESPACE index 55c7d6b99..20b219bde 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -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) diff --git a/R/epiparameter.R b/R/epiparameter.R index 856b71044..66e66451a 100644 --- a/R/epiparameter.R +++ b/R/epiparameter.R @@ -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 ) @@ -369,6 +369,38 @@ assert_epiparameter <- function(x) { invisible(x) } +#' Test whether an object is a valid `` object +#' +#' @param x An \R object. +#' +#' @return A boolean `logical` whether the object is a valid `` +#' 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 `` class #' #' @param x An `` object. diff --git a/man/test_epiparameter.Rd b/man/test_epiparameter.Rd new file mode 100644 index 000000000..205dcf014 --- /dev/null +++ b/man/test_epiparameter.Rd @@ -0,0 +1,18 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/epiparameter.R +\name{test_epiparameter} +\alias{test_epiparameter} +\title{Test whether an object is a valid \verb{} object} +\usage{ +test_epiparameter(x) +} +\arguments{ +\item{x}{An \R object.} +} +\value{ +A boolean \code{logical} whether the object is a valid \verb{} +object. +} +\description{ +Test whether an object is a valid \verb{} object +}