Skip to content

Commit

Permalink
Fix warning when par() is called without parameter (#238)
Browse files Browse the repository at this point in the history
  • Loading branch information
MLopez-Ibanez authored and lionel- committed Jan 16, 2024
1 parent ce73b80 commit bf0e738
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 30 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@

* `local_()` now works even if withr isn't attached (#207).

* `local_par()` and `with_par()` now work if you don't set any parameters
(#238).

* `defer()` is now a thin wrapper around `base::on.exit()`. This is
possible thanks to two contributions that we made to R 3.5:

Expand Down
7 changes: 6 additions & 1 deletion R/par.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ NULL

get_par <- function(...) {
new <- auto_splice(list(...))
out <- do.call(graphics::par, as.list(names(new)))
if (length(new) == 0) {
# Only retrieve settable values
out <- graphics::par(no.readonly = TRUE)
} else {
out <- do.call(graphics::par, as.list(names(new)))
}

# `par()` doesn't wrap in a list if input is length 1
if (length(new) == 1) {
Expand Down
15 changes: 0 additions & 15 deletions tests/testthat/test-local.R
Original file line number Diff line number Diff line change
Expand Up @@ -169,21 +169,6 @@ test_that("local_dir works as expected", {
expect_equal(normalizePath(getwd()), normalizePath(old))
})

test_that("local_par works as expected", {
tmp <- tempfile()

pdf(tmp)
on.exit(unlink(tmp), add = TRUE)

old <- par("pty")
local({
local_par(list(pty = "s"))
expect_equal(par("pty"), "s")
})
expect_equal(par("pty"), old)
dev.off()
})

test_that("supplying a getter to `local_()` shields against early exits", {
my_get <- function(x) {
out <- as.list(state)[names(x)]
Expand Down
27 changes: 27 additions & 0 deletions tests/testthat/test-par.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
test_that("with_par works as expected", {
local_pdf(local_tempfile())

old <- par("pty")
with_par(list(pty = "s"), {
expect_equal(par("pty"), "s")
})
expect_equal(par("pty"), old)
dev.off()
})

test_that("local_par works as expected", {
local_pdf(local_tempfile())

old <- par("pty")
local({
local_par(list(pty = "s"))
expect_equal(par("pty"), "s")
})
expect_equal(par("pty"), old)
})

test_that("empty par doesn't give warning", {
local_pdf(local_tempfile())

expect_no_warning(with_par(list(), par(pty="s")))
})
14 changes: 0 additions & 14 deletions tests/testthat/test-with.R
Original file line number Diff line number Diff line change
Expand Up @@ -161,20 +161,6 @@ test_that("with_dir works as expected", {
expect_equal(normalizePath(getwd()), normalizePath(old))
})

test_that("with_par works as expected", {
tmp <- tempfile()

pdf(tmp)
on.exit(unlink(tmp), add = TRUE)

old <- par("pty")
with_par(list(pty = "s"), {
expect_equal(par("pty"), "s")
})
expect_equal(par("pty"), old)
dev.off()
})

test_that("supplying a getter to `with_()` shields against early exits", {
my_get <- function(x) {
out <- as.list(state)[names(x)]
Expand Down

0 comments on commit bf0e738

Please sign in to comment.