From 70f48d58ce29e56cd5ed6fe7fe0d6783464575a3 Mon Sep 17 00:00:00 2001 From: Andrew Pulsipher <45372570+apulsipher@users.noreply.github.com> Date: Tue, 10 Dec 2024 10:06:00 -0700 Subject: [PATCH] Add error message when plotting a model before running the model and update tests (#56) --- R/plot_epi.R | 3 +++ inst/tinytest/test-seir.R | 2 +- inst/tinytest/test-sir.R | 2 +- inst/tinytest/test-sirconn.R | 2 +- inst/tinytest/test-sird.R | 2 +- inst/tinytest/test-sis.R | 2 +- 6 files changed, 8 insertions(+), 5 deletions(-) diff --git a/R/plot_epi.R b/R/plot_epi.R index b263a931..5ecc1ed3 100644 --- a/R/plot_epi.R +++ b/R/plot_epi.R @@ -38,6 +38,9 @@ plot_epi.epiworld_model <- function( counts_scale, ... ) { + # Check if model has been run + if (length(get_hist_total(x)$counts) <= 0) + stop("The model must be run before it can be plotted.") plot_epi( x = get_hist_total(x), diff --git a/inst/tinytest/test-seir.R b/inst/tinytest/test-seir.R index a41591a8..1224f415 100644 --- a/inst/tinytest/test-seir.R +++ b/inst/tinytest/test-seir.R @@ -26,7 +26,7 @@ expect_silent(agents_smallworld( expect_silent(verbose_off(seir_0)) expect_silent(queuing_off(seir_0)) expect_silent(initial_states(seir_0, c(.3, .5))) -expect_warning(expect_error(plot(seir_0))) # Plot fails before model is run +expect_error(plot(seir_0), "model must be run before it can be plotted") expect_silent(run(seir_0, ndays = 100, seed = 1231)) expect_silent(plot(seir_0)) # Plot succeeds after model is run diff --git a/inst/tinytest/test-sir.R b/inst/tinytest/test-sir.R index c9bcd465..fbfd3752 100644 --- a/inst/tinytest/test-sir.R +++ b/inst/tinytest/test-sir.R @@ -40,7 +40,7 @@ expect_silent(agents_smallworld( # Check model run with queuing ------------------------------------------------- expect_silent(verbose_off(sir_0)) -expect_warning(expect_error(plot(sir_0))) # Plot fails before model is run +expect_error(plot(sir_0), "model must be run before it can be plotted") expect_silent(run(sir_0, ndays = 50, seed = 1912)) expect_silent(plot(sir_0)) # Plot succeeds after model is run diff --git a/inst/tinytest/test-sirconn.R b/inst/tinytest/test-sirconn.R index 2e3074b5..cc480d83 100644 --- a/inst/tinytest/test-sirconn.R +++ b/inst/tinytest/test-sirconn.R @@ -41,7 +41,7 @@ expect_length(class(sirconn_0), 2) # Check model run with queuing ------------------------------------------------- expect_silent(verbose_off(sirconn_0)) expect_warning(queuing_on(sirconn_0)) -expect_warning(expect_error(plot(sirconn_0))) # Plot fails before model is run +expect_error(plot(sirconn_0), "model must be run before it can be plotted") expect_silent(run(sirconn_0, ndays = 100, seed = 131)) expect_silent(plot(sirconn_0)) # Plot succeeds after model is run diff --git a/inst/tinytest/test-sird.R b/inst/tinytest/test-sird.R index 4e201660..231d9279 100644 --- a/inst/tinytest/test-sird.R +++ b/inst/tinytest/test-sird.R @@ -24,7 +24,7 @@ expect_silent(agents_smallworld( # Check model run -------------------------------------------------------------- expect_silent(verbose_off(sird_0)) expect_silent(initial_states(sird_0, c(.05, .05))) -expect_warning(expect_error(plot(sird_0))) # Plot fails before model is run +expect_error(plot(sird_0), "model must be run before it can be plotted") expect_silent(run(sird_0, ndays = 100, seed = 1231)) expect_silent(plot(sird_0)) # Plot succeeds after model is run diff --git a/inst/tinytest/test-sis.R b/inst/tinytest/test-sis.R index b4c3b62d..37dfda29 100644 --- a/inst/tinytest/test-sis.R +++ b/inst/tinytest/test-sis.R @@ -22,7 +22,7 @@ expect_silent(agents_smallworld( # Check model run -------------------------------------------------------------- expect_silent(verbose_off(sis_0)) -expect_warning(expect_error(plot(sis_0))) # Plot fails before model is run +expect_error(plot(sis_0), "model must be run before it can be plotted") expect_silent(run_multiple( sis_0, ndays=100,