0
votes

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
Thanks @Florian for pointing to this. Have included a reproducible example now.Dhiraj

2 Answers

3
votes

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)
0
votes

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)