在Shiny Application中添加鼠标坐标

时间:2022-04-03 21:18:34

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.

我正在开发一个闪亮的应用程序,用户点击地图(传单地图)并根据该点击执行某些操作,例如在点击的点周围绘制一个半径为1公里的圆。此功能正常。但是,我想通过使用mapview包中的addMouseCoordinates()函数添加鼠标坐标。我过去使用过这个没问题。但是使用以下代码,我无法看到坐标。

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.

它与leafletProxy()有关吗?帮助将不胜感激。

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 个解决方案

#1


2  

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

将addMouseCoordinates调用移动到设置地图的位置(定义输出$ 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)

#2


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)

#1


2  

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

将addMouseCoordinates调用移动到设置地图的位置(定义输出$ 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)

#2


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)