我有一个闪亮的应用程序,用户可以通过DrawToolbar在传单地图上创建新点。每次设置新标记时,坐标都会添加到data.frame中。我想将这些坐标显示为新添加标记的弹出窗口。这可能不会失去拖动或移除新标记的可能性吗?
library(shiny)
library(leaflet)
library(leaflet.extras)
ui <- fluidPage(
leafletOutput("map")
)
data <- data.frame(lat = c(), lon = c())
server <- function(input, output, session) {
output$map <- renderLeaflet({
leaflet() %>% addTiles() %>%
addDrawToolbar(
targetGroup = "new_points",
polylineOptions = FALSE,
polygonOptions = FALSE,
rectangleOptions = FALSE,
circleOptions = FALSE,
editOptions = editToolbarOptions(selectedPathOptions = selectedPathOptions()))
})
observeEvent(input$map_draw_new_feature, {
click_lat <- input$map_draw_new_feature$geometry$coordinates[[2]]
click_lon <- input$map_draw_new_feature$geometry$coordinates[[1]]
data <- rbind(data, cbind(click_lat, click_lon))
print(data)
})
}
shinyApp(ui, server)
答案 0 :(得分:1)
您可以使用reactiveValues
代替全局变量来存储新标记,每次添加时都会重新绘制地图(使用弹出窗口添加所有标记):
library(shiny)
library(leaflet)
library(leaflet.extras)
ui <- fluidPage(
leafletOutput("map")
)
server <- function(input, output, session) {
data <- reactiveValues(lat = NULL, lon = NULL)
output$map <- renderLeaflet({
# Get setView parameters
new_zoom <- 2
if(!is.null(input$map_zoom)) new_zoom <- input$map_zoom
new_lat <- 0
if(!is.null(input$map_center$lat)) new_lat <- input$map_center$lat
new_lon <- 0
if(!is.null(input$map_center$lng)) new_lon <- input$map_center$lng
leaflet() %>% addTiles() %>%
setView(new_lon,new_lat,zoom = new_zoom) %>%
addDrawToolbar(
targetGroup = "new_points",
polylineOptions = FALSE,
polygonOptions = FALSE,
rectangleOptions = FALSE,
circleOptions = FALSE,
editOptions = editToolbarOptions(
selectedPathOptions = selectedPathOptions()))-> map
if (!is.null(data$lat)) {
addMarkers(map, lng=data$lon, lat=data$lat,
popup=paste("lat=", data$lat, ", lon=", data$lon))}
else map
})
observeEvent(input$map_draw_new_feature, {
click_lat <- input$map_draw_new_feature$geometry$coordinates[[2]]
click_lon <- input$map_draw_new_feature$geometry$coordinates[[1]]
data$lat <- c(data$lat,click_lat)
data$lon <- c(data$lon,click_lon)
})
}
shinyApp(ui, server)
请注意setView
调用以避免缩放到您添加的第一个标记