【问题标题】:ObserveEvent does not print out layer id after click the map点击地图后 ObserveEvent 不打印图层 id
【发布时间】:2018-09-27 14:28:58
【问题描述】:

我试图通过点击地图获取国家信息,但国家名称或 Long /lat 没有打印出来。

谁能在这个问题上启发我?

谢谢

地图来自自然地球。

library(rgdal)
library(shiny)
library(leaflet)

folder="."
country <- readOGR(dsn = folder, layer = "ne_110m_admin_0_countries", encoding="UTF-8")
country <- spTransform(country, CRS("+proj=longlat +ellps=GRS80"))

ui<-fluidPage(
  leafletOutput('mymap')
)

server <- function(input, output, session){
  # RV<-reactiveValues(Clicks=list())
  output$mymap<- renderLeaflet(
    leaflet() %>%
      addProviderTiles("CartoDB.Positron") %>%
      addPolygons(data = country, layerId= ~ADMIN, fillColor = "red", fillOpacity = 1,  color = "black", 
                  stroke = T,  weight = 1  
      )
  )
  observeEvent(input$map_shape_click, {
    p<- input$map_shape_click 
    print(p$id) # click on map, no response
  }) 
}

shinyApp(ui=ui, server=server)

【问题讨论】:

  • 我建议对您的问题进行编辑,以进行格式化:(1) 请了解 SO 的 formattingmarkdown,代码更容易阅读(至少对于许多人来说)固定宽度并可能突出显示; (2) 必须滚动浏览代码页面可能是一个障碍(MWE 中的“M”是相关的),因此在紧凑和良好风格之间存在权衡。在这种情况下,不需要许多空行(IMO),一旦删除,您的整个示例就可以在不滚动的情况下适应。
  • 我对@9​​87654325@不是很熟悉,但是你确定print没有被调用吗? $id 是否有可能无法打印? (只是一个刺......)也许stackoverflow.com/questions/42507943/…会有所帮助。

标签: r shiny leaflet


【解决方案1】:

您的地图名称是“mymap”而不是“map”。所以你需要用input$mymap_shape_click替换input$map_shape_click

library(rgdal)
library(shiny)
library(leaflet)

folder="."
country <- readOGR(dsn = folder, layer = "ne_110m_admin_0_countries", 
encoding="UTF-8")
country <- spTransform(country, CRS("+proj=longlat +ellps=GRS80"))

ui<-fluidPage(
  leafletOutput('mymap')
)

server <- function(input, output, session){
  # RV<-reactiveValues(Clicks=list())
  output$mymap<- renderLeaflet(
    leaflet() %>%
      addProviderTiles("CartoDB.Positron") %>%
      addPolygons(data = country, layerId= ~admin, fillColor = "red", 
fillOpacity = 1,  color = "black", 
                  stroke = T,  weight = 1  
      )
  )
  observeEvent(input$mymap_shape_click, {
    p<- input$mymap_shape_click 
    print(p$id) # click on map, no response
  }) 
}

shinyApp(ui=ui, server=server)

【讨论】:

  • 你好,你是我的英雄,我认为 map_shape_click 是一个内置函数。非常感谢
  • 哈哈,不客气,我也遇到过,这就是我现在知道的^^
  • 感谢@kluu。我看到的任何东西都是“map_shape_click”。所以,我认为它是 R 的关键字,如 radioButton、ui、server 等。所以,我的变量名与“variable_shape_click”不同......无论如何,谢谢愚蠢的事情浪费了我的时间!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-05
  • 1970-01-01
  • 2012-05-09
相关资源
最近更新 更多