Skip to content

Commit

Permalink
Prevent new_device = FALSE from accidentally opening a device (#235)
Browse files Browse the repository at this point in the history
Because `par("page")` will open a device if one is not already open. Fixes #234.
  • Loading branch information
hadley authored Jan 23, 2025
1 parent 094f832 commit 9b41223
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 3 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# evaluate (development version)

* `evaluate()` once again doesn't open a device if `new_device = FALSE` (#234)

# evaluate 1.0.3

# evaluate 1.0.2
Expand Down
6 changes: 6 additions & 0 deletions R/watchout.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ watchout <- function(handler = new_output_handler(),
new_device = TRUE,
debug = FALSE,
frame = parent.frame()) {

if (new_device) {
# Ensure we have a graphics device available for recording, but choose
# one that's available on all platforms and doesn't write to disk
Expand Down Expand Up @@ -43,6 +44,11 @@ watchout <- function(handler = new_output_handler(),
sink_con <- local_persistent_sink_connection(debug, frame)

capture_plot <- function(incomplete = FALSE) {
# no plots open; par("page") will open a device
if (is.null(dev.list())) {
return()
}

# only record plots for our graphics device
if (!identical(dev.cur(), dev)) {
return()
Expand Down
9 changes: 6 additions & 3 deletions tests/testthat/test-conditions.R
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,12 @@ test_that("Error can be entraced and correctly handled in outputs", {
skip_if_not_installed("knitr")
skip_if_not_installed("callr")
skip_on_cran()
# install dev version of package in temp directory
withr::local_temp_libpaths()
quick_install(pkgload::pkg_path("."), lib = .libPaths()[1])

# if not inside of R CMD check, install dev version into temp directory
if (Sys.getenv("_R_CHECK_TIMINGS_") == "") {
withr::local_temp_libpaths()
quick_install(pkgload::pkg_path("."), lib = .libPaths()[1])
}

out <- withr::local_tempfile(fileext = ".txt")

Expand Down
9 changes: 9 additions & 0 deletions tests/testthat/test-evaluate.R
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,15 @@ test_that("check_keep can integrate log option", {
expect_false(check_keep(FALSE, log = TRUE)$silence)
})

test_that("new_device = FALSE doesn't open any devices", {
graphics.off()
skip_if_not(is.null(dev.list()))

ev <- evaluate("1", new_device = FALSE)
expect_equal(dev.list(), NULL)
})


test_that("check_keep errors with bad inputs", {
expect_snapshot(error = TRUE, {
check_keep(1, "keep_message")
Expand Down

0 comments on commit 9b41223

Please sign in to comment.