0

I am working on a shiny application where a user clicks on a map (leaflet map) and based on that click, certain actions are being performed, like draw a circle of radius one km around the clicked point. This functionality works fine. However I want to add the mouse coordinates by using the addMouseCoordinates() function from the mapview package. I have used this in the past with no problems. However with the following code, I am unable to see the coordinates.

leafletProxy('incidentmap') %>%
      addCircles(lng=clng, lat=clat, group='circles',
                 weight=1, radius=input$radius, color='black', fillColor='green',
                 fillOpacity=0.2, opacity=1)%>%
      addCircles(lng=filtered$Long,lat=filtered$Lat)%>%
      addMouseCoordinates(style = "basic")

Now if I click on the map, the application crashes with the following error:

> Warning: Error in : inherits(map, "leaflet") is not TRUE Stack trace
> (innermost first):
>     75: stopifnot
>     74: addMouseCoordinates
>     73: function_list[[k]]
>     72: withVisible
>     71: freduce
>     70: _fseq
>     69: eval
>     68: eval
>     67: withVisible
>     66: %>%
>     65: observeEventHandler [/Users/dhirajkhanna/Desktop/CallAnalysis/CDR/server.R#39]
>      1: runApp ERROR: [on_request_read] connection reset by peer

Has it got something to do with leafletProxy() ? Help would be appreciated.

Here's a reproducible example:

library(shiny)
library(mapview)
library(leaflet)

ui <- fluidPage(
  leafletOutput("incidentmap")
)

server <- function(input,output,session){
  output$incidentmap <- renderLeaflet({
    leaflet() %>%
      setView(lng = 77.9568288, lat = 27.1696145, zoom=11) %>%
      addTiles(options = providerTileOptions(noWrap = TRUE))
  })
    ## Observe mouse clicks and add circles
    observeEvent(input$incidentmap_click, {
      click <- input$incidentmap_click
      clat <- click$lat
      clng <- click$lng

      leafletProxy('incidentmap') %>%
        addCircles(lng=clng, lat=clat, group='circles',
                   weight=1, radius=1000, color='black', fillColor='green',
                   fillOpacity=0.2, opacity=1)%>%
                   addMouseCoordinates(style = "basic")
  })
}

shinyApp(ui,server)
2
  • Please consider adding a reproducible example to your question, that makes it much easier to help. For some tips on how to do that, see here.
    – Florian
    Commented Feb 9, 2018 at 9:00
  • Thanks @Florian for pointing to this. Have included a reproducible example now.
    – Dhiraj
    Commented Feb 10, 2018 at 3:28

2 Answers 2

3

It works when you move the addMouseCoordinates call to where the map is set up (where you define output$incidentmap)

library(shiny)
library(mapview)
library(leaflet)

ui <- fluidPage(
  leafletOutput("incidentmap")
)

server <- function(input,output,session){
  output$incidentmap <- renderLeaflet({
    leaflet() %>%
      setView(lng = 77.9568288, lat = 27.1696145, zoom=11) %>%
      addTiles(options = providerTileOptions(noWrap = TRUE)) %>%
      addMouseCoordinates(style = "basic")
  })
  ## Observe mouse clicks and add circles
  observeEvent(input$incidentmap_click, {
    click <- input$incidentmap_click
    clat <- click$lat
    clng <- click$lng

    leafletProxy('incidentmap') %>%
      addCircles(lng=clng, lat=clat, group='circles',
                 weight=1, radius=1000, color='black', fillColor='green',
                 fillOpacity=0.2, opacity=1)
  })
}

shinyApp(ui,server)
1
  • Exactly what I was looking for! Thanks @TimSalabim
    – Dhiraj
    Commented Feb 13, 2018 at 17:44
0

Here is an attempt to fill your need. You can easily improve uppon it ..

library(leaflet)
library(mapview)
library(shiny)

ui <- fluidPage(
  leafletOutput("map1")
)

server <- function(input, output, session) {
  output$map1 <- renderLeaflet({
    leaflet() %>%  addTiles() 
  })

  observeEvent(input$map1_click, {

    click <- input$map1_click
    clat <- click$lat
    clng <- click$lng
    content <- paste(sep = "<br/>",
                     "<b>",clat, "</b>",
                     "<b>", clng, "</b>"    )
    leafletProxy('map1') %>% 
      addCircles(lng=clng, lat=clat, group='circles',
                 weight=1, radius=100, color='black', fillColor='orange',
                 fillOpacity=0.5, opacity=1)  %>% 
      addPopups(lng = clng, lat = clat, content)

  })
}
shinyApp(ui, server)
3
  • Thanks @MrSmithGoesToWashington but not quite. Your code will show the lat long of the place that has been clicked. However the user generally has the lat long of the place they wish to click already available. Hence they need to hover with the mouse to that place and then click.
    – Dhiraj
    Commented Feb 10, 2018 at 3:31
  • Sorry, that was not clear for me from your question.What do you mean by "the user generally has the lat long of the place ..." ? Is it shown in your reproductible example ? Commented Feb 12, 2018 at 8:25
  • the user receives the location of a point verbally and has to use the mouse to hover to that location on the map and click. Its not possible to show this through an example.
    – Dhiraj
    Commented Feb 12, 2018 at 15:29

Not the answer you're looking for? Browse other questions tagged or ask your own question.