Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove on.exit() from plot.epiparameter() #420

Merged
merged 3 commits into from
Nov 26, 2024
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
3 changes: 1 addition & 2 deletions .lintr
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ linters: all_linters(
default_undesirable_functions,
citEntry = "use the more modern bibentry() function",
library = NULL, # too many false positive in too many files
structure = NULL, # used for class constructor functions
par = NULL # used with on.exit() and {withrs} is not a dependency
structure = NULL # used for class constructor functions
)
),
function_argument_linter = NULL,
Expand Down
3 changes: 0 additions & 3 deletions R/plot.R
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,6 @@ plot.epiparameter <- function(x,
# capture dots
dots <- list(...)

oldpar <- graphics::par(no.readonly = TRUE)
on.exit(graphics::par(oldpar))

if (is_continuous(x)) {
main <- "Probability Density Function"
} else {
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 0 additions & 2 deletions tests/testthat/_snaps/epiparameter/epiparameter-plot.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 12 additions & 8 deletions tests/testthat/test-epiparameter.R
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,8 @@ test_that("epiparameter fails as expected", {
})

test_that("epiparameter.plot does not produce an error", {
# plotting changes global state of graphics pars so they are restored
op <- par(no.readonly = TRUE)
ebola_dist <- suppressMessages(epiparameter(
disease = "ebola",
epi_name = "incubation",
Expand All @@ -182,18 +184,19 @@ test_that("epiparameter.plot does not produce an error", {
prob_distribution_params = c(shape = 1, scale = 1)
)
))


expect_silent(plot(ebola_dist))

f <- function() plot(ebola_dist)
vdiffr::expect_doppelganger(
title = "epiparameter.plot",
fig = f
)
# restore graphics pars
par(op)
})

test_that("epiparameter.plot prints units in x-axis", {
# plotting changes global state of graphics pars so they are restored
op <- par(no.readonly = TRUE)
ebola_dist <- suppressMessages(epiparameter(
disease = "ebola",
epi_name = "incubation",
Expand All @@ -203,18 +206,19 @@ test_that("epiparameter.plot prints units in x-axis", {
),
metadata = create_metadata(units = "days")
))


expect_silent(plot(ebola_dist))

f <- function() plot(ebola_dist)
vdiffr::expect_doppelganger(
title = "epiparameter.plot units",
fig = f
)
# restore graphics pars
par(op)
})

test_that("epiparameter.plot works with non-default x-axis", {
# plotting changes global state of graphics pars so they are restored
op <- par(no.readonly = TRUE)
ebola_dist <- suppressMessages(epiparameter(
disease = "ebola",
epi_name = "incubation",
Expand All @@ -223,14 +227,12 @@ test_that("epiparameter.plot works with non-default x-axis", {
prob_distribution_params = c(shape = 1, scale = 1)
)
))

expect_silent(
plot(
ebola_dist,
xlim = c(0, 20)
)
)

f <- function() {
plot(
ebola_dist,
Expand All @@ -241,6 +243,8 @@ test_that("epiparameter.plot works with non-default x-axis", {
title = "epiparameter.plot non-default range",
fig = f
)
# restore graphics pars
par(op)
})

test_that("new_epiparameter works with minimal viable input", {
Expand Down
Loading