Skip to content

Commit

Permalink
feat: clear/remove conditional group
Browse files Browse the repository at this point in the history
  • Loading branch information
trafficonese committed Jan 12, 2025
1 parent c61ad0f commit 1b4fedf
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 4 deletions.
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export(antpathOptions)
export(arrowheadOptions)
export(clearAntpath)
export(clearArrowhead)
export(clearConditionalLayers)
export(clearFuture)
export(clearHexbin)
export(clearHistory)
Expand Down Expand Up @@ -75,6 +76,7 @@ export(playbackOptions)
export(reachabilityOptions)
export(removeAntpath)
export(removeArrowhead)
export(removeConditionalLayer)
export(removeEasyprint)
export(removeItemContextmenu)
export(removePlayback)
Expand Down
20 changes: 19 additions & 1 deletion R/layergroupconditional.R
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ layerGroupConditionalDependency <- function() {
#' breweries91 <- st_as_sf(breweries91)
#' lines <- st_as_sf(atlStorms2005)
#' polys <- st_as_sf(leaflet::gadmCHE)
#' groups <- c("storms","breweries","polys")
#' groups <- c("atlStorms","breweries","gadmCHE")
#'
#' leaflet() %>%
#' addTiles() %>%
Expand All @@ -65,3 +65,21 @@ addLayerGroupConditional <- function(map, groups = NULL, conditions = NULL) {
groups, conditions
)
}

#' removeConditionalLayer
#'
#' @inheritParams addLayerGroupConditional
#' @family LayerGroupConditional Plugin
#' @export
removeConditionalLayer <- function(map, groups) {
invokeMethod(map, getMapData(map), "removeConditionalLayer", groups)
}

#' clearConditionalLayers
#'
#' @inheritParams addLayerGroupConditional
#' @family LayerGroupConditional Plugin
#' @export
clearConditionalLayers <- function(map) {
invokeMethod(map, getMapData(map), "clearConditionalLayers")
}
14 changes: 12 additions & 2 deletions inst/examples/conditional_app.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ library(leaflet.extras2)
breweries91 <- st_as_sf(breweries91)
lines <- st_as_sf(atlStorms2005)
polys <- st_as_sf(leaflet::gadmCHE)
groups <- c("storms","breweries","polys")
groups <- c("atlStorms","breweries","gadmCHE")

ui <- fluidPage(
leafletOutput("map", height = 900)
leafletOutput("map", height = 900),
actionButton("remove", "Remove by Group"),
actionButton("clear", "Clear")
)

server <- function(input, output, session) {
Expand All @@ -30,5 +32,13 @@ server <- function(input, output, session) {
addLayersControl(overlayGroups = groups,
options = layersControlOptions(collapsed=FALSE))
})
observeEvent(input$clear, {
leafletProxy("map") %>%
clearConditionalLayers()
})
observeEvent(input$remove, {
leafletProxy("map") %>%
removeConditionalLayer(groups=groups[1])
})
}
shinyApp(ui, server)
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ LeafletWidget.methods.addLayerGroupConditional = function(groups, conditions) {

const map = this;
let conditionalGroup = L.layerGroup.conditional();
map.conditionalGroup = conditionalGroup;

// Loop through each group
groups.forEach(function(group) {
Expand Down Expand Up @@ -39,3 +40,23 @@ LeafletWidget.methods.addLayerGroupConditional = function(groups, conditions) {
}, 500);

};


LeafletWidget.methods.removeConditionalLayer = function(groups) {
const map = this;
if (!Array.isArray(groups)) {
groups = [groups];
}
groups.forEach(function(group) {
let layer = map.layerManager.getLayerGroup(group);
if (layer && map.conditionalGroup) {
map.conditionalGroup.removeConditionalLayer(layer)
} else {
console.warn("Layer not found " + group);
}
})
};

LeafletWidget.methods.clearConditionalLayers = function() {
this.conditionalGroup.clearConditionalLayers()
};
6 changes: 5 additions & 1 deletion man/addLayerGroupConditional.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions man/clearConditionalLayers.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions man/removeConditionalLayer.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 1b4fedf

Please sign in to comment.