Friday, 23 June 2017

r - Add mouse coordinates in Shiny Application



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)


Answer



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)

No comments:

Post a Comment

casting - Why wasn&#39;t Tobey Maguire in The Amazing Spider-Man? - Movies &amp; TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...