如何单击传单地图,创建标记,然后在单击 R 中的其他位置时删除该标记

How do I click on a leaflet map, create a marker, and then delete that marker when I click elsewhere in R

我创建了一个闪亮的应用程序,它显示数据框中点的传单地图。

我想允许用户单击地图上的任意位置以获取有关附近点的一些信息并在该点上留下标记。

他们可能想点击其他地方。当他们点击别处时,我希望留下一个新标记,并删除旧标记。

我写了一个可以工作的 Shiny 应用程序,但我无法让它工作。

  1. 我尝试使用 clearMarkers,但这会删除所有标记,包括我创建的标记和基础数据框中的标记。

  2. 我试过指定点击点的id,这样clearMarkers可能只是删除那个,但我不知道谁能找到点击点的id。

我怎样才能让它工作?

这是我的玩具代码:

library(shiny)
library(sp)
library(shinydashboard)
library(leaflet)

#### Make a spatial data frame 
lats<-c(37.38,39)
lons<-c(-94,-95,-96)
df<-data.frame(cbind(lons,lats))
coordinates(df)<-~lons+lats

#### Define UI for application that draws a histogram
ui <- dashboardPage(

    dashboardHeader(
    ),

    # Sidebar layout with input and output definitions 
    dashboardSidebar(
    ),

    # Main panel for displaying outputs 
    dashboardBody(
                     h2("My Map", align="center"),
                     h5("Click anywhere to draw a circle", align="center"),
                     leafletOutput("mymap", width="100%", height="500px")
        ),
    )



#### Define server logic required to draw a histogram
server <- function(input, output) {

    output$mymap <- renderLeaflet({
                 m = leaflet(df,width="100%",height="100%") %>% 
                 addTiles()    %>%
                 addCircleMarkers()
    })

    observeEvent(input$mymap_click, {

        click <- input$mymap_click
        text<-paste("Latitude ", round(click$lat,2), "Longtitude ", round(click$lng,2))

        proxy <- leafletProxy("mymap")

        ## This displays the pin drop circle
        proxy %>% 
            #clearPopups() %>%
            #clearMarkers(layerId=input$mymap_click$id) %>%
            #addPopups(click$lng, click$lat) %>%
            addCircles(click$lng, click$lat, radius=100, color="red")

    })


}

# Run the application 
shinyApp(ui = ui, server = server)

您可以将 addCirclesgroup 参数与 clearGroup 一起使用 -

library(shiny)
library(sp)
library(shinydashboard)
library(leaflet)

#### Make a spatial data frame 
lats<-c(37.38,39)
lons<-c(-94,-95,-96)
df<-data.frame(cbind(lons,lats))
coordinates(df)<-~lons+lats

#### Define UI for application that draws a histogram
ui <- dashboardPage(

    dashboardHeader(
    ),

    # Sidebar layout with input and output definitions 
    dashboardSidebar(
    ),

    # Main panel for displaying outputs 
    dashboardBody(
                     h2("My Map", align="center"),
                     h5("Click anywhere to draw a circle", align="center"),
                     leafletOutput("mymap", width="100%", height="500px")
        ),
    )



#### Define server logic required to draw a histogram
server <- function(input, output) {

    output$mymap <- renderLeaflet({
                 m = leaflet(df,width="100%",height="100%") %>% 
                 addTiles()    %>%
                 addCircleMarkers()
    })


    observeEvent(input$mymap_click, {

        click <- input$mymap_click
        text<-paste("Latitude ", round(click$lat,2), "Longtitude ", round(click$lng,2))

        proxy <- leafletProxy("mymap")

        ## This displays the pin drop circle
        proxy %>% 
            clearGroup("new_point") %>%
            #clearMarkers(layerId=input$mymap_click$id) %>%
            #addPopups(click$lng, click$lat) %>%
            addCircles(click$lng, click$lat, radius=100, color="red", group = "new_point")

    })


}

# Run the application 
shinyApp(ui = ui, server = server)