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

Pet*_*ete 3 r shiny r-leaflet

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

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

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

我已经编写了一个工作闪亮的应用程序,但我无法让它工作。

  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)
Run Code Online (Sandbox Code Playgroud)

Shr*_*ree 6

您可以为此使用group参数-addCirclesclearGroup

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)
Run Code Online (Sandbox Code Playgroud)