如何将信息从Leaflet弹出窗口传递到Shiny输出? [英] How can I pass info from Leaflet popup to Shiny output?

查看:211
本文介绍了如何将信息从Leaflet弹出窗口传递到Shiny输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个根据shapefile和人口统计数据创建的传单地图(myMap).地图显示多边形(县).当我独自运行此地图时-或从Shiny中渲染它时-我可以单击一个县,然后在创建的弹出窗口中显示该县ID.

I have a Leaflet map (myMap) that I created from a shapefile and demographic data. The map displays the polygons (counties). When I run this map on its own - or when I render it from within Shiny - I can click on a county and the county ID is displayed in the popup that I created.

我一直想弄清楚的是如何从Shiny中访问我的弹出窗口值(ID).例如,在我的Shiny应用程序中,我想单击县,然后将县ID输出到文本显示或存储为变量.

What I am stuck trying to figure out is how to access my popup values (ID) from within Shiny. For example, in my Shiny app I'd like to click on the county and have the county ID outputted to a text display or stored as a variable.

以下是相关代码:

ui <- dashboardPage(
  dashboardBody(fluidRow(
    box(width = 9, status = "info", title = "CountyMap",
    leafletOutput("myMap"))
)

server <- function(input, output) {
  output$myMap <- renderLeaflet({map2})

  observe({
  event <- input$myMap_shape_click
  if (is.null(event))
  return()

  print(map2$county)  # I know that's not correct,
                      # but I want the county id from my leaflet popup!

  val <- map2$county  # Obviously not correct either, 
  })                  # but I would like to store this data

如果有帮助,我从Shiny(上图)中调用的Leaflet地图看起来像这样,其中"mapable"是一个大的空间多边形数据框:

In case this helps, the Leaflet map that I call from Shiny (above) looks something like this, where "mapable" is a large spatial polygons data frame:

popup <- paste0("ID: ", mapable$countyID)

map2 <-leaflet() %>%
     addPolygons(data = mapable, 
                 popup = popup
     ) 

任何对正确方向的想法或推动将不胜感激!

Any thoughts or pushes in the right direction would be greatly appreciated!

推荐答案

感谢user5219763向我提示了layerId参数的作用!我回到了传单地图,并为layerID添加了矢量化参数.在我的情况下,我从用于地图的大空间多边形数据框"的"GEO_ID"列中创建了一个值向量.

Thanks to user5219763 for tipping me off to what the layerId argument is for! I went back to my leaflet map and added vectorized argument for layerID. In my case I created a vector of values from the "GEO_ID" column in the "large spatial polygons data frame" that I used for the map.

geoID <- as.vector(mapable$GEOID)

map2 <-leaflet() %>%
     addPolygons(data = mapable, 
                 layerId = geoID,
                 popup = popup
     )

当我运行有光泽的应用程序并单击一个多边形(县)时,我可以测试是否也传递了layerId:

When I run the shiny app and click on a polygon (county), I can test that the layerId is also being passed:

  observe({
    event <- input$myMap_shape_click
    if (is.null(event))
      return()
    print(event)      
  })

这篇关于如何将信息从Leaflet弹出窗口传递到Shiny输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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