title: How do I click on a leaflet map, create a marker, and then delete that marker when I click elsewhere in R
body: <p>I have created a shiny application the displays a leaflet map of points in a data frame. </p>
  
  <p>I want to allow the user to click anywhere on the map to get some information on nearby points and leave a marker on that point. </p>
  
  <p>It is possible that they might want to click elsewhere. When they click elsewhere, I would like a new marker to be left, and the old marker to be deleted. </p>
  
  <p>I have written a working Shiny application but I cannot get it to work. </p>
  
  <ol>
  <li><p>I tried using clearMarkers, but this deletes ALL of the marker, both the one that I created and the ones in the underlying dataframe. </p></li>
  <li><p>I tried specifying the id of the clicked point so that clearMarkers might simply just delete that one, but I have no idea who to find out the id of the clicked point. </p></li>
  </ol>
  
  <p>How can I get this to work?</p>
  
  <p>Here is my toy code:</p>
  
  <pre><code>library(shiny)
  library(sp)
  library(shinydashboard)
  library(leaflet)
  
  #### Make a spatial data frame 
  lats&lt;-c(37.38,39)
  lons&lt;-c(-94,-95,-96)
  df&lt;-data.frame(cbind(lons,lats))
  coordinates(df)&lt;-~lons+lats
  
  #### Define UI for application that draws a histogram
  ui &lt;- dashboardPage(
  
      dashboardHeader(
      ),
  
      # Sidebar layout with input and output definitions 
      dashboardSidebar(
      ),
  
      # Main panel for displaying outputs 
      dashboardBody(
                       h2("My Map", align="center"),
                       h5("Click anywhere to draw a circle", align="center"),
                       leafletOutput("mymap", width="100%", height="500px")
          ),
      )
  
  
  
  #### Define server logic required to draw a histogram
  server &lt;- function(input, output) {
  
      output$mymap &lt;- renderLeaflet({
                   m = leaflet(df,width="100%",height="100%") %&gt;% 
                   addTiles()    %&gt;%
                   addCircleMarkers()
      })
  
      observeEvent(input$mymap_click, {
  
          click &lt;- input$mymap_click
          text&lt;-paste("Latitude ", round(click$lat,2), "Longtitude ", round(click$lng,2))
  
          proxy &lt;- leafletProxy("mymap")
  
          ## This displays the pin drop circle
          proxy %&gt;% 
              #clearPopups() %&gt;%
              #clearMarkers(layerId=input$mymap_click$id) %&gt;%
              #addPopups(click$lng, click$lat) %&gt;%
              addCircles(click$lng, click$lat, radius=100, color="red")
  
      })
  
  
  }
  
  # Run the application 
  shinyApp(ui = ui, server = server)
  </code></pre>