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)