Skip to content
This repository was archived by the owner on Jan 30, 2025. It is now read-only.

Commit

Permalink
Test that plot() does not change the user's graphical parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
nfrerebeau committed May 15, 2023
1 parent 367adb1 commit b2366f4
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
1 change: 1 addition & 0 deletions R/ternary_plot.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ setMethod(
panel.first = NULL, panel.last = NULL, ...) {

## Save and restore graphical parameters
## pty: square plotting region, independent of device size
old_par <- graphics::par(pty = "s", no.readonly = TRUE)
on.exit(graphics::par(old_par), add = TRUE)

Expand Down
3 changes: 2 additions & 1 deletion cran-comments.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@

0 errors | 0 warnings | 1 note

* This is a new release.
* This is a resubmission.
* We checked that the plotting functions do not change the user's graphical parameters.
45 changes: 45 additions & 0 deletions tests/testthat/test-par.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
test_that("Plot", {
skip_on_cran()

## Capture and cleanup plot output
path <- file.path(tempdir(), "Rplot.pdf")
on.exit(unlink(path))
pdf(file = path)

## Save graphical parameters
old_par <- graphics::par(no.readonly = TRUE)

## Plot...
ternary_plot(NULL)

## ...changes the user's graphical parameters...
new_par <- graphics::par(no.readonly = TRUE)
# expect_identical(new_par, old_par) # Fails

dev.off()

## ...but these are the dimensions of the plotting region
## (so it should be OK?)
keys <- new_par[!(new_par %in% old_par)]
expect_named(keys, c('pin', 'plt', 'usr', 'yaxp'))
})
test_that("Pairs", {
skip_on_cran()

## Capture and cleanup plot output
path <- file.path(tempdir(), "Rplot.pdf")
on.exit(unlink(path))
pdf(file = path)

## Save graphical parameters
old_par <- graphics::par(no.readonly = TRUE)

## Plot...
ternary_pairs(lava)

## ...does not change the user's graphical parameters
new_par <- graphics::par(no.readonly = TRUE)
expect_identical(new_par, old_par)

dev.off()
})

0 comments on commit b2366f4

Please sign in to comment.