diff --git a/.lintr b/.lintr index 35d11eee8..e2ca0f34a 100644 --- a/.lintr +++ b/.lintr @@ -5,6 +5,12 @@ linters: linters_with_tags( implicit_integer_linter = NULL, extraction_operator_linter = NULL, todo_comment_linter = NULL, - function_argument_linter = NULL + function_argument_linter = NULL, + indentation_linter = NULL, # unstable as of lintr 3.1.0 + # Use minimum R declared in DESCRIPTION or fall back to current R version. + # Install etdev package from https://github.com/epiverse-trace/etdev + backport_linter(if (length(x <- etdev::extract_min_r_version())) x else getRversion()) + ) +exclusions: list( + "tests/testthat.R" = list(unused_import_linter = Inf) ) - diff --git a/R/epidist_utils.R b/R/epidist_utils.R index 7b730703e..b60f6e114 100644 --- a/R/epidist_utils.R +++ b/R/epidist_utils.R @@ -546,6 +546,12 @@ clean_epidist_params <- function(prob_dist_params, ...) { #' @keywords internal clean_epidist_params.gamma <- function(prob_dist_params) { + # if unparameterised return named vector of NAs + if (isTRUE(is.na(prob_dist_params))) { + prob_dist_params <- c(shape = NA_real_, scale = NA_real_) + return(prob_dist_params) + } + # if shape and rate are provided convert to shape and scale if (all(c("shape", "rate") %in% names(prob_dist_params))) { prob_dist_params[["rate"]] <- 1 / prob_dist_params[["rate"]] @@ -580,6 +586,12 @@ clean_epidist_params.gamma <- function(prob_dist_params) { #' @keywords internal clean_epidist_params.lnorm <- function(prob_dist_params) { + # if unparameterised return named vector of NAs + if (isTRUE(is.na(prob_dist_params))) { + prob_dist_params <- c(meanlog = NA_real_, sdlog = NA_real_) + return(prob_dist_params) + } + # if mu and sigma are provided convert to meanlog and sdlog if (all(c("mu", "sigma") %in% names(prob_dist_params))) { @@ -616,6 +628,12 @@ clean_epidist_params.lnorm <- function(prob_dist_params) { #' @keywords internal clean_epidist_params.weibull <- function(prob_dist_params) { + # if unparameterised return named vector of NAs + if (isTRUE(is.na(prob_dist_params))) { + prob_dist_params <- c(shape = NA_real_, scale = NA_real_) + return(prob_dist_params) + } + if (all(c("shape", "scale") %in% names(prob_dist_params))) { # remove class attribute from prob_dist_params prob_dist_params <- unclass(prob_dist_params) @@ -638,6 +656,12 @@ clean_epidist_params.weibull <- function(prob_dist_params) { #' @keywords internal clean_epidist_params.nbinom <- function(prob_dist_params) { + # if unparameterised return named vector of NAs + if (isTRUE(is.na(prob_dist_params))) { + prob_dist_params <- c(mean = NA_real_, dispersion = NA_real_) + return(prob_dist_params) + } + if (all(c("n", "p") %in% names(prob_dist_params))) { # convert prob to mean @@ -682,6 +706,12 @@ clean_epidist_params.nbinom <- function(prob_dist_params) { #' @keywords internal clean_epidist_params.geom <- function(prob_dist_params) { + # if unparameterised return named NA + if (isTRUE(is.na(prob_dist_params))) { + prob_dist_params <- c(prob = NA_real_) + return(prob_dist_params) + } + # if mean is provided convert to prob if ("mean" %in% names(prob_dist_params)) { prob_dist_params[["mean"]] <- 1 / prob_dist_params[["mean"]] @@ -732,6 +762,12 @@ clean_epidist_params.geom <- function(prob_dist_params) { #' @keywords internal clean_epidist_params.pois <- function(prob_dist_params) { + # if unparameterised return named NA + if (isTRUE(is.na(prob_dist_params))) { + prob_dist_params <- c(mean = NA_real_) + return(prob_dist_params) + } + if (names(prob_dist_params) %in% c("mean", "l")) { names(prob_dist_params) <- "mean" diff --git a/tests/testthat/test-epidist_utils.R b/tests/testthat/test-epidist_utils.R index ad77bee72..6a5addbb8 100644 --- a/tests/testthat/test-epidist_utils.R +++ b/tests/testthat/test-epidist_utils.R @@ -92,6 +92,33 @@ test_that("clean_epidist_params works for default method", { ) }) +test_that("clean_epidist_params works for unparameterised (NA), (#161)", { + params <- NA + class(params) <- "gamma" + res <- clean_epidist_params(prob_dist_params = params) + expect_identical(res, c(shape = NA_real_, scale = NA_real_)) + + class(params) <- "lnorm" + res <- clean_epidist_params(prob_dist_params = params) + expect_identical(res, c(meanlog = NA_real_, sdlog = NA_real_)) + + class(params) <- "weibull" + res <- clean_epidist_params(prob_dist_params = params) + expect_identical(res, c(shape = NA_real_, scale = NA_real_)) + + class(params) <- "nbinom" + res <- clean_epidist_params(prob_dist_params = params) + expect_identical(res, c(mean = NA_real_, dispersion = NA_real_)) + + class(params) <- "geom" + res <- clean_epidist_params(prob_dist_params = params) + expect_identical(res, c(prob = NA_real_)) + + class(params) <- "pois" + res <- clean_epidist_params(prob_dist_params = params) + expect_identical(res, c(mean = NA_real_)) +}) + test_that("create_epidist_region works as expected", { region <- create_epidist_region( continent = "Europe",