R传单中的标记鼠标单击事件以实现闪亮 [英] Marker mouse click event in R leaflet for shiny

查看:29
本文介绍了R传单中的标记鼠标单击事件以实现闪亮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 R 中的传单地图中的标记上接收鼠标单击事件?我正在使用 RStudio/leaflet 并通过 Shiny 运行.

How can I receive a mouse click event on a marker in a leaflet map in R? I'm using the RStudio/leaflet and running through Shiny.

我想获取标记(例如 ID)的值并使用它来更新侧边栏面板.

I'd like to get the value of a marker (e.g., ID) and use that to update a sidebarPanel.

推荐答案

你想使用 input$MAPID_marker_click.请参阅下面的示例.

You want to use input$MAPID_marker_click. See the example below.

library(shiny)
library(leaflet)

latitude <- c(35.94077, 35.83770, 35.84545, 35.81584, 35.79387, 36.05600)
longitude <- c(-78.58010, -78.78084, -78.72444, -78.62568, -78.64262, -78.67600)
radius<-c(15, 12, 12, 12, 12, 15)
ids<-c("a", "b", "c", "d", "e", "f")

shinyApp(
  ui = fluidPage(
    fluidRow(
      leafletMap(
        "map", "100%", 400,
        initialTileLayer = "//{s}.tiles.mapbox.com/v3/jcheng.map-5ebohr46/{z}/{x}/{y}.png",
        initialTileLayerAttribution = HTML('Maps by <a href="http://www.mapbox.com/">Mapbox</a>'),
        options=list(
          center = c(37.45, -93.85),
          zoom = 4,
          maxBounds = list(list(17, -180), list(59, 180))))),
    fluidRow(verbatimTextOutput("Click_text"))),
  server = function(input, output, session){
        map = createLeafletMap(session, 'map')
        session$onFlushed(once=T, function(){

          map$addCircleMarker(lat = latitude, 
                              lng = longitude, 
                              radius = radius, 
                              layerId=ids)
        })        

        observe({
          click<-input$map_marker_click
          if(is.null(click))
            return()
          text<-paste("Lattitude ", click$lat, "Longtitude ", click$lng)
          text2<-paste("You've selected point ", click$id)
          map$clearPopups()
          map$showPopup( click$lat, click$lng, text)
          output$Click_text<-renderText({
            text2
          })

        })

  }
)

addCircleMarker 函数中的参数layerIdids 传递给click$id.

The argument layerId in the addCircleMarker function passes the ids to the click$id.

这篇关于R传单中的标记鼠标单击事件以实现闪亮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆