ObserveEvent 单击地图后不打印图层 id

问题描述 投票:0回答:1

我试图通过单击地图来获取国家/地区信息,但国家/地区名称或经/纬度未打印出来。

有人可以告诉我这个问题吗?

地图来自 Natural Earth。

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)
r shiny r-leaflet
1个回答
6
投票

您的地图名称是“mymap”而不是“map”。所以您需要将

input$map_shape_click
替换为
input$mymap_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)
© www.soinside.com 2019 - 2024. All rights reserved.