title: dynamic ggplot layers in shiny with nearPoints()
body: <p>I'm familiar with the basics of shiny but struggling with something here. I would like to be able to add a ggplot layer when a point is clicked to highlight that point. I know this is possible with ggvis and there is a nice example in the gallery, but I would like to be able to use <code>nearPoints()</code> to capture the click as ui input.</p>
  
  <p>I have tried something (see below) which works apart from the ggplot layer appears and then disappears. I have tried all kinds of edits to this with <code>reactive()</code>, <code>eventReactive()</code> and so on.</p>
  
  <p>Any help is much appreciated...</p>
  
  <pre><code>library(shiny)
  library(ggplot2)
  
  shinyApp(
    ui = shinyUI(
          plotOutput("plot", click = "clicked")
      ),
  
    server = shinyServer(function(input, output) {
      output$plot &lt;- renderPlot({
        ggplot(mtcars, aes(x = mpg, y = wt)) +
          geom_point() +
          geom_point(data = nearPoints(mtcars, input$clicked), colour = "red", size = 5)
      })
    })
  )
  </code></pre>
  
  <p>I think I understand conceptually why this doesn't work. The plot has a dependency on <code>input$clicked</code> which means that when <code>input$clicked</code> changes the plot re-renders but this in turn resets <code>input$clicked</code>. Bit of a catch 22 situation.</p>