Skip to content

Send pending plot changes before R waits for user input - #1484

Merged
juliasilge merged 1 commit into
mainfrom
plots-flush-before-input-request
Nov 17, 2026
Merged

juliasilge merged 1 commit into
mainfrom
plots-flush-before-input-request

Conversation

@juliasilge

Copy link
Copy Markdown
Member

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():

Screenshot 2026-11-16 at 11 19 14 AM

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(), or devAskNewPage() prompt, the Plots pane showed the current page without its last layers. The late update came after the user answered the prompt, so demo(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 fixes readline() but it does not fix the devAskNewPage() prompt. plot.default(), barplot(), hist(), and similar functions call dev.hold() before plot.new(), and R shows the prompt inside plot.new(). At the prompt the device is held, so process_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:

  • Send pending changes each time a hold starts. This is the simplest correct fix for the prompt but plot.default() and hist() 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.
  • At an input request, send changes and ignore the hold. This breaks dev.hold() and then a page drawn entirely while held appears at the prompt.
  • Set the device's newFrameConfirm callback. This covers only the devAskNewPage() prompt, not readline() or menu(), plus it also has the same hold problem.
  • Record the display list each time a hold starts with unsent changes. This keeps dev.hold() exact, but it adds a recordPlot() call to plot(); 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_changes is 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 one Cell copy per hold.
  • has_unrecorded_changes is 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 with par(mfrow = c(1, 2))) or renders the same image again at dev.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 on process_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.

@isabelizimm isabelizimm left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@juliasilge

Copy link
Copy Markdown
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.

@juliasilge
juliasilge merged commit c505fbd into main Nov 17, 2026
19 checks passed
@juliasilge
juliasilge deleted the plots-flush-before-input-request branch November 17, 2026 20:39
@github-actions github-actions Bot locked and limited conversation to collaborators Nov 17, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants