Repository navigation
Send pending plot changes before R waits for user input - #1484
Merged
Merged
Conversation
isabelizimm
approved these changes
Nov 17, 2026
Contributor
There was a problem hiding this comment.
not blocking, but maybe something to simplify in the future:
DeviceContext now has seven interacting flags for hold/notify state (has_changes, is_new_page, should_render, is_notification_deferred, held_pages, and the two new ones). Might be worth a follow-up to model this as an explicit state enum so the transitions are easier to follow.
Member
Author
|
@isabelizimm, that's a really good suggestion. I am going to take that as a quick follow up as outlined in posit-dev/positron#17394. |
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 subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
Addresses posit-dev/positron#7680
When R waits for user input during an execute request, the frontend now gets every plot change that is not hidden by
dev.hold():Before this change, ark sent plot changes only at the end of the request, when a hold was released, or when a new page started. At a
readline(),menu(), ordevAskNewPage()prompt, the Plots pane showed the current page without its last layers. The late update came after the user answered the prompt, sodemo(graphics)showed partial plots that then jumped.This base R example shows the problem:
local({ devAskNewPage(TRUE) plot(1:10) points(5, 5, col = "red", pch = 16, cex = 4) plot(1:5) })At the second "Hit to see next plot:" prompt, page 1 did not show the red point.
Note that R asks before it draws each new page, including the first page on a new device. At each prompt, the Plots pane shows the previous page, and the next page comes after the user answers. This is the same behavior as other R devices. This PR makes sure that the page at the prompt is complete.
Why the fix needs state in the device
This PR does add some additional complexity here, so let me walk through this. If we called
process_changes()before the input request, that fixesreadline()but it does not fix thedevAskNewPage()prompt.plot.default(),barplot(),hist(), and similar functions calldev.hold()beforeplot.new(), and R shows the prompt insideplot.new(). At the prompt the device is held, soprocess_changes()only defers again.The changes on the page at that time were drawn before the hold. On a screen device they are already visible, and only drawing during a hold is hidden. Ark needs to send these changes at the prompt but should not actually send drawing that the hold hides. A hold level alone can't tell the two apart, so the device needs to know when a change was drawn relative to the hold.
I looked at a couple of alternatives:
plot.default()andhist()hold the device on every call. For example,points(); hist(x, add = TRUE)in a loop then renders a PNG twice per iteration, not once. That would be a fairly high cost on the common path to fix an uncommon one.dev.hold()and then a page drawn entirely while held appears at the prompt.newFrameConfirmcallback. This covers only thedevAskNewPage()prompt, notreadline()ormenu(), plus it also has the same hold problem.dev.hold()exact, but it adds arecordPlot()call toplot(); points(); plot(). Removing that cost needs even more state than this PR adds.This PR adds two flags to
DeviceContext. Neither flag renders or records on its own:has_pre_hold_changesis set when a hold starts and the page has unsent changes. At an input request while held, ark sends the page only if this flag is set. As a result, a page drawn entirely while held stays hidden. The common path costs oneCellcopy per hold.has_unrecorded_changesis set when anything is drawn and cleared when the current page is recorded. After ark sends a page at a prompt, ark uses this flag to decide if the page needs another update when the hold is released. Without it, ark either loses drawing done under the hold (for example, a second panel withpar(mfrow = c(1, 2))) or renders the same image again atdev.flush().There is one trade-off here. If a page was partly drawn before a hold and ark has no recording since the hold, ark records at the prompt and drawing done during the hold then appears at the prompt, not at
dev.flush(). No drawing is lost, and I believe that pattern has got be pretty rare. The doc comment onprocess_changes_before_input()describes it.Out of scope
A Plots pane resize at an input prompt does not render the plot again until the execute request completes. Positron does not send render requests while the kernel is busy, and the amalthea Shell thread does not read comm messages during an execute request. I logged posit-dev/positron#17359 for this one.