I am working on Shiny app. One of the plots in my app is an area plot of Chicago evictions over time. I have permanent annotations on the plot for the peak and most recent values in the data (2012 and 2019, respectively). I would like to make it so that when you hover over the plot with your mouse, another annotation for whatever year your mouse is closest to also appears. Right now, the annotations based on the hover input flash briefly and then disappear.
Here's a paired-down version of the app with just the troublesome plot so you can see its behavior, and here's the relevant code:
# In ui
plotOutput("evict_plot", hover = "hover")
# Current code
output$evict_plot <- renderPlot({
yr <- ifelse(is.null(input$hover), 0, round(input$hover$x))
evict_base +
{if(yr == 2010)annotate("text", x = 2010.57, y = 25296,
label = 'atop("23,058 evictions", bold("81.1% back rent cases"))',
family = "lato", parse = TRUE)} +
< additional if statements >
I thought using a reactive value might solve the problem...
# Attempt using reactive value
output$evict_plot <- renderPlot({
yr <- reactiveVal()
yr(0)
observeEvent(input$hover,
yr(round(input$hover$x)))
evict_base +
{if(yr() == 0 | yr() == 2010)annotate("text", x = 2010.57, y = 25296,
label = 'atop("23,058 evictions", bold("81.1% back rent cases"))',
family = "lato", parse = TRUE)} +
< additional if statements >
But it didn't. When I first open the app with the reactive value implementation, 2010 was annotated, as I would expect per the code above:
But, as soon as I hovered my mouse over the plot, the app started to hang:
Chicago evictions over time, Shiny hanging
Does anyone have ideas about how I can use the hover input to add an annotation for a year that stays put until you move your mouse closer to a different year (vs. just flashing briefly, like it does now)? I'm going to experiment with hoverOpts, but I feel like there should we a way to do it with just the basic hover syntax, since I don't need any of the additional functionality overOpts offers.
Thank you!
