在Shiny Application中添加鼠标坐标

时间:2018-02-09 08:18:14

标签: r shiny leaflet

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

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")

现在,如果我点击地图,应用程序崩溃时会出现以下错误:

> 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

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

这是一个可重复的例子:

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 个答案:

答案 0 :(得分:2)

当您将addMouseCoordinates调用移至设置地图的位置(您在其中定义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 :(得分:0)

这是尝试满足您的需求。你可以很容易地改善它...

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)