更新时间:2023-01-08 16:15:58
我将向您展示一个较小的示例.
I'm going to show you a smaller example of how this works.
注意事项
lat
,lng
和id
值id
值是您使用layerId
参数在addMarkers()
调用中分配的值layerId
id
来过滤数据.
lat
, lng
and id
valuesid
value is that which you assign inside the addMarkers()
call using the layerId
argumentid
to filter your data, assuming you've used an id value from the data as the layerId
在此示例中,我使用的是googleway
软件包随附的数据集
In this example I'm using a data set supplied with my googleway
package
library(shiny)
library(leaflet)
library(googleway)
ui <- fluidRow(
leafletOutput(outputId = "map"),
tableOutput(outputId = "table")
)
server <- function(input, output){
## I'm using data from my googleway package
df <- googleway::tram_stops
## define the layerId as a value from the data
output$map <- renderLeaflet({
leaflet() %>%
addTiles() %>%
addMarkers(data = df, lat = ~stop_lat, lng = ~stop_lon, layerId = ~stop_id)
})
## observing a click will return the `id` you assigned in the `layerId` argument
observeEvent(input$map_marker_click, {
click <- input$map_marker_click
## filter the data and output into a table
output$table <- renderTable({
df[df$stop_id == click$id, ]
})
})
}
shinyApp(ui, server)