Restore graphics state after plot key errors - #2617
Open
LeonidasZhak wants to merge 2 commits into
Open
LeonidasZhak wants to merge 2 commits into
LeonidasZhak wants to merge 2 commits into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Handle the error path where plotting a key that is too narrow leaves the graphics device in an invalid state, and sync the PR branch with current
mainso it is reviewable again.Thanks to maintainers
Thanks for maintaining sf and for the note on #2611. The reproducer there made this easier to narrow down.
Issue or motivation
Closes #2611. With long factor labels and
key.pos = 2,plot.sf()can report an invalid graphics state. After that, retrying on the same device with a wider key can still fail because the previous error left the device/layout state broken.Root cause
The failing key plot happens inside
.image_scale_factor(). Its error handler then calledstrwidth()after the device was already in a bad state, which could mask the usefulkey.width too smallmessage. Separately,plot.sf()reset the layout before restoringpar(), so an error while resetting the layout could prevent restoration of the previous graphical parameters.Change
This computes the fallback key width before the risky key plot, so the error handler does not query text widths after failure. It also restores
par()before resetting the layout, and starts a new plot only on the incomplete error path to get the device back to a usable state. The latest update only merges currentmainand resolves theNEWS.mdconflict.Tests
R_LIBS_USER=... Rscript --vanilla -e '.libPaths(c(Sys.getenv("R_LIBS_USER"), .libPaths())); library(sf); testthat::test_file("tests/testthat/test-plot.R", reporter="summary")'passed: 9 assertions.git diff origin/main --checkpassed for the final PR diff.R_LIBS_USER=... R CMD build --no-manual --no-build-vignettes .passed and builtsf_1.1-3.tar.gz._R_CHECK_FORCE_SUGGESTS_=false _R_CHECK_CRAN_INCOMING_REMOTE_=false R CMD check --as-cran --no-manual --ignore-vignettes --no-build-vignettes sf_1.1-3.tar.gzexited 0 with 2 NOTEs.The NOTEs were the expected no-prebuilt-vignette-index note from not building vignettes locally, and saved-output numeric/platform differences in the spatial tests under my local GDAL 3.13.1, GEOS 3.14.1, and PROJ 9.8.1 stack. The new
plot.Routput matched.Scope
This does not change key sizing policy, regenerate documentation, or try to make the automatically suggested key width wide enough for every device. It only keeps the error path recoverable, preserves the existing useful
key.width too smallguidance, and restores the PR branch to a mergeable state.