How to stop refreshing plotOutput until input values are changed

The example shiny app below shows a ggplot in which you can click anywhere within the plot area and see the text annotation "you clicked here" pop up on that coordinate. However, the text disappears almost instantly after showing up. How can I prevent it from disappearing until a new input (i.e., mouse click) comes in?

library(shiny)
library(ggplot2)

ui <- fluidPage(
    
    plotOutput("example_plot", click = "mouse_click")

)

server <- function(input, output) {

    output$example_plot <- renderPlot({
        
        ggplot(mtcars, mapping = aes(x = disp, y = hp)) + 
            geom_point() +
            annotate(geom = "text", 
                     x = input$mouse_click$x, 
                     y = input$mouse_click$y, 
                     label = "you clicked here")
        
    })
}

shinyApp(ui = ui, server = server)

You can use a plotting library that has interactive features build in, for example plotly

library(shiny)
library(ggplot2)
library(plotly)

ui <- fluidPage(
    plotlyOutput("example_plot")
)

server <- function(input, output) {

    output$example_plot <- renderPlotly({
        
        p <- ggplot(mtcars, mapping = aes(x = disp, y = hp)) + 
            geom_point()
        ggplotly(p)
        
    })
}

shinyApp(ui = ui, server = server)
1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.

I didn't even know such a fantastic package existed. Thank you!