基于下面的评论,我明确在空间数据框中细分了经/纬度。
已添加
addCircleMarkers( ~ longitude, ~ latitude)
已添加
observeEvent(input$map_marker_click, {
p <- input$map_marker_click
print(p)
})
但是,当我单击标记时,控制台中什么都没有显示,所以我仍然感到困惑。
修改后的代码
# Click on circle and get info
library(shiny)
library(leaflet)
ui <- fluidPage(
leafletOutput("mymap"),
fluidRow(verbatimTextOutput("click_text"))
)
server <- function(input, output, session) {
# Create tree geometries
tree_1g <- st_point(c(-79.2918671415814, 43.6760766531298))
tree_2g <- st_point(c(-79.4883669334101, 43.6653747165064))
tree_3g <- st_point(c(-79.2964680812039, 43.7134458013647))
# Create sfc object with multiple sfg objects
points_sfc <- st_sfc(tree_1g, tree_2g, tree_3g, crs = 4326)
# Create tree attributes
data <- data.frame (
layerId = c("001", "002", "003"),
address = c(10, 20, 30),
street = c("first", "second", "third"),
tname = c("oak", "elm", "birch"),
latitude = c(43.6760766531298, 43.6653747165064, 43.7134458013647),
longitude = c(-79.2918671415814, -79.4883669334101, -79.2964680812039)
)
tree_data <- st_sf(data, geometry = points_sfc)
output$mymap <- renderLeaflet({
leaflet(data = tree_data) %>%
addProviderTiles(providers$Stamen.Watercolor) %>%
# Centre the map in the middle of Toronto
setView(lng = -79.384293,
lat = 43.685,
zoom = 11) %>%
addCircleMarkers( ~ longitude, ~ latitude)
})
observeEvent(input$map_marker_click, {
p <- input$map_marker_click
print(p)
})
}
shinyApp(ui, server)
当用户单击每个标记时,我希望在地图下方显示一些相关信息。基于此较早的post,我尝试了此操作。但是,当我单击标记时,什么也没有发生。可能与我不了解如何将标记与layerId相关联有关。
# Click on circle and get info
library(shiny)
library(leaflet)
ui <- fluidPage(
leafletOutput("mymap"),
fluidRow(verbatimTextOutput("click_text"))
)
server <- function(input, output, session) {
# Create tree geometries
tree_1g <- st_point(c(-79.2918671415814, 43.6760766531298))
tree_2g <- st_point(c(-79.4883669334101, 43.6653747165064))
tree_3g <- st_point(c(-79.2964680812039, 43.7134458013647))
# Create sfc object with multiple sfg objects
points_sfc <- st_sfc(tree_1g, tree_2g, tree_3g, crs = 4326)
# Create tree attributes
data <- data.frame (
layerId = c("001", "002", "003"),
address = c(10, 20, 30),
street = c("first", "second", "third"),
tname = c("oak", "elm", "birch")
)
tree_data <- st_sf(data, geometry = points_sfc)
output$mymap <- renderLeaflet({
leaflet(data = tree_data) %>%
addProviderTiles(providers$Stamen.Watercolor) %>%
# Centre the map in the middle of Toronto
setView(lng = -79.384293,
lat = 43.685,
zoom = 11) %>%
addCircleMarkers()
})
observe({
click <- input$map_marker_click
if(is.null(click))
return()
address <- paste("Address: ", click$street)
output$click_text <- renderText({
address
})
})
}
shinyApp(ui, server)
答案 0 :(得分:1)
当您“观察”地图上的某物时,您需要参考正在观察的地图。您可以使用此结构
output$<map_id>_event_to_observe
因此,在您的示例中,您的map_id
是mymap
,因此您需要使用
observeEvent(input$mymap_marker_click, {
p <- input$mymap_marker_click
print(p)
})