How to use `event_register` for plotly mouse-click events

I try to implement a visual analog scale, where the user clicks on a line indicating her sensation.
I know how to do this in default graphics, but for other reasons I want to use plotly here. In the example below, this error message is shown:

The 'plotly_click' event tied a source ID of 'vas' is not registered. In order to obtain this event data, please add event_register(p, 'plotly_click') to the plot (p) that you wish to obtain event data from.

(Correction: see my reply below)
How do I register the mouse event? There is almost no documentation left after plotly does no longer support R directly.

library(shiny)
library(plotly)

ui = fluidPage(
  plotlyOutput("vas")
)

server = function(input, output, session) {
  eventClick = reactive(event_data("plotly_click", source = "vas"))

  observeEvent(eventClick(), {
    # In the end, I need the x-coordinate here
    print("click")
  })

  output$vas = renderPlotly({
    plot_ly() |>
      add_trace(
         type = "scatter",
         mode = "line",
         x = c(0,10),
         y = c(0,0)
      ) |>
      plotly::layout(
        xaxis = list(showgrid = FALSE, zeroline = TRUE,
                   showspikes = TRUE, spikesnap = "cursor"),
        yaxis = list(visible = FALSE)
      ) |>
      config(displayModeBar = FALSE)  |>
      event_register("plotly_click")
  })
}

shinyApp(ui, server)

Partially corrected: The confusing warning is gone when I use:

plot_ly(source = "vas")

And register_eventis not required, because click is registered by default, as the docs say. I only had added it because of the warning.

I get the event, when I click on the left origin or the point to the right. So the question is reduced to:

  • How to get the event anywhere without inserting invisible data points.

Having found some ugly workarounds, I give up. Strange that an elaborate tool such as plotly cannot do simple things.

I took that "ugly" personally :sweat_smile:..

I don't know your usecase but I thought maybe dragging existing points is a prettier option: