Skip to content

Support locator() and identify() in the Plots pane - #1486

Draft
juliasilge wants to merge 6 commits into
mainfrom
feature/locator
Draft

juliasilge wants to merge 6 commits into
mainfrom
feature/locator

Conversation

@juliasilge

@juliasilge juliasilge commented Nov 18, 2026 •

Copy link
Copy Markdown
Member

locator(), identify(), and grid::grid.locator() now work in the Positron Plots pane. When R asks for a point, the Plots pane shows the plot with a crosshair cursor and a prompt. The user clicks the plot, and R gets the point in user coordinates, as with other on-screen devices. The Finish button, Esc, or an interrupt ends the call.

Before this change, these functions did not work at all. The Ark device wraps the png or ragg device, and neither device has a usable locator callback.

How it works

The design is based on file.choose(), which asks Positron for a file with a blocking frontend request on stdin. The graphics device now has a locator callback and for each click, DeviceContext::locate() does these steps:

  1. It sends the current page to the frontend, so a locator() call in the same request as plot() has a plot to click on.
  2. It sends a pre-render of the page at the device size, unless the frontend already has one from this request.
  3. It sends a blocking locate_plot_point request on the UI comm with the plot comm ID and the image size, then it waits for the reply on stdin.

The reply is the clicked point as fractions of the image (0 to 1, from the top left corner), or null when the user finishes. Ark changes the fractions to device coordinates with the device bounds. The image goes on IOPub and the request goes on stdin, so the request can arrive first. Positron accepts clicks only on a loaded image of the requested size.

locator() returns at once, as if the user finished, in a notebook session, without a UI comm, when the pre-render fails, during a resize replay, or when an interrupt is pending.

identify() draws its labels in graphical input mode, so hook_mode() detects them from the mode switch that R makes after each label. On an interrupt, the callback waits for the interrupt to be pending and then raises it once, so the rest of the expression does not run.

Stale replies on stdin

Before this change, a late reply to an interrupted stdin request answered the next request. For example, after an interrupted file.choose() or readline(), a late reply became the answer to the next readline(). Amalthea now discards these replies:

  • A comm reply with a parent header must have the pending request as its parent.
  • An input reply whose parent is a different input_request is a late reply, so Amalthea discards it.
  • Replies with no parent header are still accepted, because some Jupyter frontends (for example jupyter_client) send none.

Positron sends each reply with the header of its request as the parent. The reply handling also uses log_err() instead of unwrap().

Known limitations

  • Points that locator(type = "p") draws, and labels that identify() draws, show only after the call returns. R adds the call to the display list when the call returns, and Ark renders from the display list.
  • An interrupt during locator() does not stop the rest of the top-level input. For example, with locator(1) on one line and cat("after") on the next line, an interrupt ends locator() and then cat("after") runs. readline() has the same behavior. To prevent this, put the code in braces: { locator(1); cat("after") }.
  • In a console session with no UI comm (for example jupyter console), dev.capabilities("locator") is TRUE, but locator() returns NULL at once.
  • While R waits for a click, Ark does not answer render requests from the frontend. A resize or a change of the sizing policy takes effect after the click.

Validation Steps

With the companion PR:

  • In the R console, run plot(1:10); locator(2).
  • Make sure that the prompt shows above the plot and that the cursor is a crosshair.
  • Click the plot at two points. R prints the coordinates of the points that you clicked.
  • Run identify(1:10), click two points, and then click Finish. R prints the indices, and the labels show on the plot.

Positron Release Notes

New Features

Bug Fixes

  • A late reply to an interrupted readline() or file.choose() no longer becomes the answer to the next prompt.

juliasilge added a commit to posit-dev/positron that referenced this pull request Nov 18, 2026
Also brings in posit-dev/ark#1484 and posit-dev/ark#1485.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

1 participant