Skip to content

Restore graphics state after plot key errors - #2617

Open
LeonidasZhak wants to merge 2 commits into
r-spatial:mainfrom
LeonidasZhak:codex/sf-2611-graphics-state
Open

LeonidasZhak wants to merge 2 commits into
r-spatial:mainfrom
LeonidasZhak:codex/sf-2611-graphics-state

Conversation

@LeonidasZhak

@LeonidasZhak LeonidasZhak commented Aug 31, 2026

Copy link
Copy Markdown

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 main so 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 called strwidth() after the device was already in a bad state, which could mask the useful key.width too small message. Separately, plot.sf() reset the layout before restoring par(), 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 current main and resolves the NEWS.md conflict.

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 --check passed for the final PR diff.
  • R_LIBS_USER=... R CMD build --no-manual --no-build-vignettes . passed and built sf_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.gz exited 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.R output 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 small guidance, and restores the PR branch to a mergeable state.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Invalid graphic state with long labels

1 participant