删除R传单中的特定图层

时间:2019-09-19 15:41:02

标签: r shiny r-leaflet

我的ShinyApp由两个区域组成,分别是地图和图表。 您可以在图表中选择一个区域,该区域将更新与所选图表区域相对应的地图核心上的数据。 因此,地图使用了一个观察对象和一个letletletProxy来添加过滤后的数据,从而使用ClearMarkers()来删除以前的过滤后的数据。

问题是::我还有overlayGroup的另一个MarkersLayer部分,并且可以由overlayGroupWidget显示,但没有显示。 为什么?由于ClearMarkers()会删除地图上的所有标记(T0New和T1New)

因此,我想删除特定的图层,它们是T0New和MapData。 我尝试了removeMarker()和clearGroup(),但没有成功...

有什么想法吗?

这是我的示例代码数据示例:

library(shiny)
library(leaflet)
library(leaflet.extras)
library(tidyverse)
library(sf)



#Create T0New data
lat <- c(49.823, 58.478, 57.478, 45.823)
lng <- c(-10.854,-10.854,2.021,2.02)
date_start_min <- c(125,135,168,149)
T0New <- data.frame(lat,lng)

#Create T1New data
lat <- c(48.956, 56.356, 57.445, 45.253)
lng <- c(-9.762,-8.884,1.971,2.17)
T1New <- data.frame(lat,lng)

ui <- fluidPage(

              leafletOutput("map", height = "50vh"),
              plotOutput("distribPlot", height = "47vh",
                         brush = brushOpts(id = "distribPlot_brush", direction = "x", resetOnNew = FALSE))
  )


server <- function(input, output, session) {



  #filtrer les données par attribut du graphique
  filteredGraphData <- reactive({

    noSelection <- TRUE
    currentlyFiltered <- T0New

    if(!is.null(input$distribPlot_brush)){
      thisSel <- input$distribPlot_brush
      currentlyFiltered <- currentlyFiltered %>% 
        filter(date_start_min >= thisSel$xmin, date_start_min <= thisSel$xmax)
      noSelection <- FALSE
    }
    if(!noSelection){
      return(currentlyFiltered)
    }
  })

  #Sortie map
  output$map <- renderLeaflet({
    leaflet()%>%
      addLayersControl(
        position = "bottomright",
        overlayGroups = "T1New",
        options = layersControlOptions(collapsed = F)
      ) %>% 
      hideGroup("T1New") %>% 
      addProviderTiles(providers$CartoDB.Positron) %>% 
      addCircleMarkers(
        lat = T0New$lat,
        lng = T0New$lng,
        radius = 4,
        color = 'red',
        stroke = FALSE,
        fillOpacity = 1
      )%>%
      addCircleMarkers(
        lat = T1New$lat,
        lng = T1New$lng,
        radius = 5,
        color = 'blue',
        stroke = FALSE,
        fillOpacity = 1,
        group = "T1New"
      )
  })

  observe({
    if(length(filteredGraphData()) > 1){
      mapData <- filteredGraphData()
      mapProxy <- leafletProxy("map", session = session, data = c(mapData, T0New))
      mapProxy %>%
        clearMarkers() %>% 
        addCircleMarkers(
          data = T0New,
          lat = T0New$lat,
          lng = T0New$lng,
          radius = 1,
          color = 'black',
          stroke = FALSE,
          fillOpacity = 1
        ) %>%
        addCircleMarkers(
          data = mapData,
          lat = mapData$lat,
          lng = mapData$lng,
          radius = 4,
          color = 'red',
          stroke = FALSE,
          fillOpacity = 1
        )
    }else{
      mapProxy <- leafletProxy("map", session = session, data = T0New)
      mapProxy %>%
        clearMarkers() %>% 
        addCircleMarkers(
          radius = 4,
          color = 'red',
          stroke = FALSE,
          fillOpacity = 1
        )
    }
  })

  #Sortie graph
  output$distribPlot <- renderPlot({

    distribPlot <- ggplot(T0New,aes(date_start_min)) +
      geom_density(col = "#053144", fill = "#43a2ca", alpha = 0.3, adjust = 0.75)

    return(distribPlot)
  })

}

# Create Shiny app ----
shinyApp(ui = ui, server = server)

1 个答案:

答案 0 :(得分:0)

我终于能够找到一个解决方案:它位于 clearGroup()中 我不知道为什么它最初没有奏效,她是:

library(shiny)
library(leaflet)
library(leaflet.extras)
library(tidyverse)
library(sf)



#Create T0New data
lat <- c(49.823, 58.478, 57.478, 45.823)
lng <- c(-10.854,-10.854,2.021,2.02)
date_start_min <- c(125,135,168,149)
T0New <- data.frame(lat,lng)

#Create T1New data
lat <- c(48.956, 56.356, 57.445, 45.253)
lng <- c(-9.762,-8.884,1.971,2.17)
T1New <- data.frame(lat,lng)

ui <- fluidPage(

              leafletOutput("map", height = "50vh"),
              plotOutput("distribPlot", height = "47vh",
                         brush = brushOpts(id = "distribPlot_brush", direction = "x", resetOnNew = FALSE))
  )


server <- function(input, output, session) {



  #filtrer les données par attribut du graphique
  filteredGraphData <- reactive({

    noSelection <- TRUE
    currentlyFiltered <- T0New

    if(!is.null(input$distribPlot_brush)){
      thisSel <- input$distribPlot_brush
      currentlyFiltered <- currentlyFiltered %>% 
        filter(date_start_min >= thisSel$xmin, date_start_min <= thisSel$xmax)
      noSelection <- FALSE
    }
    if(!noSelection){
      return(currentlyFiltered)
    }
  })

  #Sortie map
  output$map <- renderLeaflet({
    leaflet()%>%
      addLayersControl(
        position = "bottomright",
        overlayGroups = "T1New",
        options = layersControlOptions(collapsed = F)
      ) %>% 
      hideGroup("T1New") %>% 
      addProviderTiles(providers$CartoDB.Positron) %>% 
      addCircleMarkers(
        lat = T0New$lat,
        lng = T0New$lng,
        radius = 4,
        color = 'red',
        stroke = FALSE,
        fillOpacity = 1,
        group = 'A'
      )%>%
      addCircleMarkers(
        lat = T1New$lat,
        lng = T1New$lng,
        radius = 5,
        color = 'blue',
        stroke = FALSE,
        fillOpacity = 1,
        group = "T1New"
      )
  })

  observe({
    if(length(filteredGraphData()) > 1){
      mapData <- filteredGraphData()
      mapProxy <- leafletProxy("map", session = session, data = c(mapData, T0New))
      mapProxy %>%
        clearGroup('A') %>% 
        addCircleMarkers(
          data = T0New,
          lat = T0New$lat,
          lng = T0New$lng,
          radius = 1,
          color = 'black',
          stroke = FALSE,
          fillOpacity = 1,
          group = 'A'
        ) %>%
        addCircleMarkers(
          data = mapData,
          lat = mapData$lat,
          lng = mapData$lng,
          radius = 4,
          color = 'red',
          stroke = FALSE,
          fillOpacity = 1,
          group = 'reactive'
        )
    }else{
      mapProxy <- leafletProxy("map", session = session, data = T0New)
      mapProxy %>%
        clearGroup('A') %>% 
        addCircleMarkers(
          radius = 4,
          color = 'red',
          stroke = FALSE,
          fillOpacity = 1,
          group = 'A'
        )
    }
  })

  #Sortie graph
  output$distribPlot <- renderPlot({

    distribPlot <- ggplot(T0New,aes(date_start_min)) +
      geom_density(col = "#053144", fill = "#43a2ca", alpha = 0.3, adjust = 0.75)

    return(distribPlot)
  })

}

# Create Shiny app ----
shinyApp(ui = ui, server = server)