How can I annotate a shiny plot based on mouse click?

,

I would like to draw a vertical line on a plot (base R or ggplot2) based on a mouse click in the plot. Clicking invalidates the plot (so does resizing) so the annotation flashes on plot but then disappears. invalidateLater() will allow the annotation to display after a second, for a second, but that's it. What I want is "invalidate after a new mouse click." In the example below, the annotation and textOutput() persists for a second. If I remove the invalidateLater(1000) line, the textOutput() does persist until the next click, but not the plot. I've seen other questions that seem to be the same problem but no answers (even "it can't be done"). Thanks.

library(shiny)

ui <- fluidPage(plotOutput("distPlot", click = "plot_click"),
                textOutput("x_click"))

server <- function(input, output) {
   
   x    <- faithful$waiting
   output$x_click <- renderPrint({
      input$plot_click$x
   })
   output$distPlot <- renderPlot({
      hist(x)
      # add vertical line at click
      abline(v = isolate(input$plot_click$x), col = 'red')
      invalidateLater(1000)
   })
}
shinyApp(ui = ui, server = server)

The issue here is that the click argument in plotOutput apparently results in two calls to both renderPrint and renderPlot, with the second call having a null value for input$plot_click. A somewhat complicated workaround for this is to make the position of the vertical line a reactive value and add an event observer that only updates it when the new value is not null. The following seems to work correctly.

library(shiny)

ui <- fluidPage(plotOutput("distPlot", click = "plot_click"),
                textOutput("x_click"))

server <- function(input, output) {
  
  x <- faithful$waiting
  v0 <- reactiveVal(NULL)
  observeEvent(input$plot_click$x,
               {
                  ifelse(is.null(input$plot_click), v0(), input$plot_click$x) |> v0()
               }
  )
  output$x_click <- renderPrint({
    v0()
  })
  output$distPlot <- renderPlot({
    hist(x)
    # add vertical line at click
    abline(v = v0(), col = 'red')
  })
}
shinyApp(ui = ui, server = server)

Note that I removed the invalidateLater call.