From bfdc654f3d90cb5766fbe9cfc81b5428825d8a1b Mon Sep 17 00:00:00 2001 From: Sebastian Gatscha Date: Sun, 12 Jan 2025 14:47:33 +0100 Subject: [PATCH 1/7] feat: init conditoinal, multiple groups per condition --- NAMESPACE | 1 + R/layergroupconditional.R | 67 +++++++++++ inst/examples/conditional_app.R | 34 ++++++ ...leaflet.layergroup.conditional-bindings.js | 45 ++++++++ .../leaflet.layergroup.conditional.js | 104 ++++++++++++++++++ man/addLayerGroupConditional.Rd | 60 ++++++++++ 6 files changed, 311 insertions(+) create mode 100644 R/layergroupconditional.R create mode 100644 inst/examples/conditional_app.R create mode 100644 inst/htmlwidgets/lfx-conditional/leaflet.layergroup.conditional-bindings.js create mode 100644 inst/htmlwidgets/lfx-conditional/leaflet.layergroup.conditional.js create mode 100644 man/addLayerGroupConditional.Rd diff --git a/NAMESPACE b/NAMESPACE index 697d440..fb8b91e 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -16,6 +16,7 @@ export(addItemContextmenu) export(addLabelgun) export(addLatLngMoving) export(addLayerGroupCollision) +export(addLayerGroupConditional) export(addLeafletsync) export(addLeafletsyncDependency) export(addMapkeyMarkers) diff --git a/R/layergroupconditional.R b/R/layergroupconditional.R new file mode 100644 index 0000000..5e6d77f --- /dev/null +++ b/R/layergroupconditional.R @@ -0,0 +1,67 @@ +layerGroupConditionalDependency <- function() { + list( + htmltools::htmlDependency( + "lfx-conditional", + version = "1.0.0", + src = system.file("htmlwidgets/lfx-conditional", package = "leaflet.extras2"), + script = c( + "leaflet.layergroup.conditional.js", + "leaflet.layergroup.conditional-bindings.js" + ) + ) + ) +} + +#' addLayerGroupConditional +#' +#' @param map A map widget object created from \code{\link[leaflet]{leaflet}}. +#' @param groups A character vector of layer group names already added to the map. +#' These layer groups will be conditionally displayed based on the specified \code{conditions}. +#' @param conditions A named list where: +#' - Each **name** is a JavaScript function (using \code{\link[leaflet]{JS}}) defining a condition for displaying a layer group. +#' - Each **value** corresponds to a layer group name (or names) from the \code{layers} parameter. +#' Example: +#' \preformatted{ +#' condition = list( +#' "(zoomLevel) => zoomLevel < 4" = "group1", +#' "(zoomLevel) => zoomLevel >= 4 && zoomLevel < 6" = "group2", +#' "(zoomLevel) => zoomLevel >= 6" = c("group3", "group4") +#' ) +#' } +#' +#' @seealso \url{https://github.com/Solfisk/Leaflet.LayerGroup.Conditional?tab=readme-ov-file} for more details about the plugin. +#' @family LayerGroupConditional Plugin +#' @export +#' @examples +#' library(leaflet) +#' library(sf) +#' library(leaflet.extras2) +#' +#' breweries91 <- st_as_sf(breweries91) +#' lines <- st_as_sf(atlStorms2005) +#' polys <- st_as_sf(leaflet::gadmCHE) +#' groups <- c("storms","breweries","polys") +#' +#' leaflet() %>% +#' addTiles() %>% +#' leafem::addMouseCoordinates() %>% +#' addPolylines(data = lines, label = ~Name, group = groups[1]) %>% +#' addCircleMarkers(data = breweries91, label = ~brewery, group = groups[2]) %>% +#' addPolygons(data = polys, label = ~NAME_1, group = groups[3]) %>% +#' addLayerGroupConditional(groups = groups, +#' conditions = list( +#' "(zoomLevel) => zoomLevel < 4" = groups[1], +#' "(zoomLevel) => zoomLevel >= 4 & zoomLevel < 6 " = groups[2], +#' "(zoomLevel) => zoomLevel >= 6" = groups[3] +#' )) %>% +#' hideGroup(groups) %>% +#' addLayersControl(overlayGroups = groups, +#' options = layersControlOptions(collapsed=FALSE)) +#' +addLayerGroupConditional <- function(map, groups = NULL, conditions = NULL) { + map$dependencies <- c(map$dependencies, layerGroupConditionalDependency()) + invokeMethod( + map, getMapData(map), "addLayerGroupConditional", + groups, conditions + ) +} diff --git a/inst/examples/conditional_app.R b/inst/examples/conditional_app.R new file mode 100644 index 0000000..bb1563f --- /dev/null +++ b/inst/examples/conditional_app.R @@ -0,0 +1,34 @@ +library(shiny) +library(leaflet) +library(sf) +library(leaflet.extras2) + +breweries91 <- st_as_sf(breweries91) +lines <- st_as_sf(atlStorms2005) +polys <- st_as_sf(leaflet::gadmCHE) +groups <- c("storms","breweries","polys") + +ui <- fluidPage( + leafletOutput("map", height = 900) +) + +server <- function(input, output, session) { + output$map <- renderLeaflet({ + leaflet() %>% + addTiles() %>% + leafem::addMouseCoordinates() %>% + addPolylines(data = lines, label = ~Name, group = groups[1]) %>% + addCircleMarkers(data = breweries91, label = ~brewery, group = groups[2]) %>% + addPolygons(data = polys, label = ~NAME_1, group = groups[3]) %>% + addLayerGroupConditional(groups = groups, + conditions = list( + "(zoomLevel) => zoomLevel < 4" = groups[1], + "(zoomLevel) => zoomLevel >= 4 & zoomLevel < 6 " = groups[2], + "(zoomLevel) => zoomLevel >= 6" = c(groups[1], groups[3]) + )) %>% + hideGroup(groups) %>% + addLayersControl(overlayGroups = groups, + options = layersControlOptions(collapsed=FALSE)) + }) +} +shinyApp(ui, server) diff --git a/inst/htmlwidgets/lfx-conditional/leaflet.layergroup.conditional-bindings.js b/inst/htmlwidgets/lfx-conditional/leaflet.layergroup.conditional-bindings.js new file mode 100644 index 0000000..7c62921 --- /dev/null +++ b/inst/htmlwidgets/lfx-conditional/leaflet.layergroup.conditional-bindings.js @@ -0,0 +1,45 @@ +/* global LeafletWidget, $, L, Shiny, HTMLWidgets */ +LeafletWidget.methods.addLayerGroupConditional = function(groups, conditions) { + + var map = this; + var conditionalGroup = L.layerGroup.conditional(); + console.log("groups"); console.log(groups); console.log("conditions"); console.log(conditions) + + // Loop through each group + groups.forEach(function(group) { + // Loop through conditions for each group + Object.keys(conditions).forEach(function(condition) { + var groupList = conditions[condition]; + if (!Array.isArray(groupList)) { + groupList = [groupList]; + } + groupList.forEach(function(group) { + var layer = map.layerManager.getLayerGroup(group); + if (!layer) { + console.warn("Layer not found in group " + group); + return; + } + // Add the layer with the associated condition + conditionalGroup.addConditionalLayer(eval(condition), layer); + }) + }); + }); + + // Add the conditional group to the map + conditionalGroup.addTo(map) + console.log("conditionalGroup");console.log(conditionalGroup); + + // Set up the zoom handler to update conditional layers + var zoomHandler = function() { + var zoomLevel = map.getZoom(); + console.log("zoomHandler: " + zoomLevel) + conditionalGroup.updateConditionalLayers(zoomLevel); + }; + map.on("zoomend", zoomHandler); + + // Set initial state of conditional layers + setTimeout(function() { + zoomHandler() + }, 200); + +}; diff --git a/inst/htmlwidgets/lfx-conditional/leaflet.layergroup.conditional.js b/inst/htmlwidgets/lfx-conditional/leaflet.layergroup.conditional.js new file mode 100644 index 0000000..e997c7b --- /dev/null +++ b/inst/htmlwidgets/lfx-conditional/leaflet.layergroup.conditional.js @@ -0,0 +1,104 @@ +L.LayerGroup.Conditional = L.LayerGroup.extend({ + + initialize: function (layers, options) { + this._conditionalLayers = {}; + L.LayerGroup.prototype.initialize.call(this, layers, options); + }, + + // @method addConditionalLayer(conditionFunction: (object) => boolean, layer: Layer): this + // Adds the given layer to the group with a condition. + addConditionalLayer: function (conditionFunction, layer) { + var id = this.getLayerId(layer); + + this._conditionalLayers[id] = { + "condition": conditionFunction, + "layer": layer, + "active": false, + } + + return this; + }, + + // @method removeConditionalLayer(layer: Layer): this + // Removes the given conditional layer from the group. + // @alternative + // @method removeConditionalLayer(id: Number): this + // Removes the conditional layer with the given internal ID from the group. + removeConditionalLayer: function (layer) { + var id = layer in this._conditionalLayers ? layer : this.getLayerId(layer); + + if (this._conditionalLayers[id] && this._conditionalLayers[id].active) { + this.removeLayer(id); + } + + delete this._conditionalLayers[id]; + + return this; + }, + + // @method hasConditionalLayer(layer: Layer): Boolean + // Returns `true` if the given layer is currently added to the group with a condition, regardless of whether it is active + // @alternative + // @method hasConditionalLayer(id: Number): Boolean + // Returns `true` if the given internal ID is currently added to the group with a condition, regardless of whether it is active. + hasConditionalLayer: function (layer) { + if (!layer) { return false; } + var layerId = typeof layer === 'number' ? layer : this.getLayerId(layer); + return layerId in this._conditionalLayers; + }, + + // @method isConditionalLayerActive(layer: Layer): Boolean + // Returns `true` if the given layer is currently added to the group with a condition, and is currently active + // @alternative + // @method hasConditionalLayer(id: Number): Boolean + // Returns `true` if the given internal ID is currently added to the group with a condition, and is currently active. + isConditionalLayerActive: function (layer) { + if (!layer) { return false; } + var layerId = typeof layer === 'number' ? layer : this.getLayerId(layer); + return (layerId in this._conditionalLayers) && this._conditionalLayers[layerId].active; + }, + + + // @method clearConditionalLayers(): this + // Removes all the conditional layers from the group. + clearConditionalLayers: function () { + for (var i in this._conditionalLayers) { + this.removeConditionalLayer(i); + } + return this; + }, + + // @method getConditionalLayers(): Layer[] + // Returns an array of all the conditional layers added to the group. + getConditionalLayers: function () { + var layers = []; + for (var i in this._conditionalLayers) { + layers.push(this._conditionalLayers[i].layer); + } + return layers; + }, + + // @method updateConditionalLayers(o: object): this + // Update the status of all conditional layers, passing an optional argument to each layer's condition function. + updateConditionalLayers: function(o) { + for (var i in this._conditionalLayers) { + var condLayer = this._conditionalLayers[i]; + var active = condLayer.condition(o); + if (active && !condLayer.active) { + this.addLayer(condLayer.layer); + } else if (condLayer.active && !active) { + this.removeLayer(condLayer.layer); + } + condLayer.active = active; + } + + return this; + } +}); + + +// @factory L.layerGroup.Conditional(layers?: Layer[], options?: Object) +// Create a conditional layer group, optionally given an initial set of non-conditinoal layers and an `options` object. +L.layerGroup.conditional = function (layers, options) { + return new L.LayerGroup.Conditional(layers, options); +}; \ No newline at end of file diff --git a/man/addLayerGroupConditional.Rd b/man/addLayerGroupConditional.Rd new file mode 100644 index 0000000..0ce48b0 --- /dev/null +++ b/man/addLayerGroupConditional.Rd @@ -0,0 +1,60 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/layergroupconditional.R +\name{addLayerGroupConditional} +\alias{addLayerGroupConditional} +\title{addLayerGroupConditional} +\usage{ +addLayerGroupConditional(map, groups = NULL, conditions = NULL) +} +\arguments{ +\item{map}{A map widget object created from \code{\link[leaflet]{leaflet}}.} + +\item{groups}{A character vector of layer group names already added to the map. +These layer groups will be conditionally displayed based on the specified \code{conditions}.} + +\item{conditions}{A named list where: +- Each **name** is a JavaScript function (using \code{\link[leaflet]{JS}}) defining a condition for displaying a layer group. +- Each **value** corresponds to a layer group name (or names) from the \code{layers} parameter. +Example: +\preformatted{ + condition = list( + "(zoomLevel) => zoomLevel < 4" = "group1", + "(zoomLevel) => zoomLevel >= 4 && zoomLevel < 6" = "group2", + "(zoomLevel) => zoomLevel >= 6" = c("group3", "group4") + ) +}} +} +\description{ +addLayerGroupConditional +} +\examples{ +library(leaflet) +library(sf) +library(leaflet.extras2) + +breweries91 <- st_as_sf(breweries91) +lines <- st_as_sf(atlStorms2005) +polys <- st_as_sf(leaflet::gadmCHE) +groups <- c("storms","breweries","polys") + +leaflet() \%>\% +addTiles() \%>\% + leafem::addMouseCoordinates() \%>\% + addPolylines(data = lines, label = ~Name, group = groups[1]) \%>\% + addCircleMarkers(data = breweries91, label = ~brewery, group = groups[2]) \%>\% + addPolygons(data = polys, label = ~NAME_1, group = groups[3]) \%>\% + addLayerGroupConditional(groups = groups, + conditions = list( + "(zoomLevel) => zoomLevel < 4" = groups[1], + "(zoomLevel) => zoomLevel >= 4 & zoomLevel < 6 " = groups[2], + "(zoomLevel) => zoomLevel >= 6" = groups[3] + )) \%>\% + hideGroup(groups) \%>\% + addLayersControl(overlayGroups = groups, + options = layersControlOptions(collapsed=FALSE)) + +} +\seealso{ +\url{https://github.com/Solfisk/Leaflet.LayerGroup.Conditional?tab=readme-ov-file} for more details about the plugin. +} +\concept{LayerGroupConditional Plugin} From c61ad0fc895ddd0f4004478de167d0b7f4bb518b Mon Sep 17 00:00:00 2001 From: Sebastian Gatscha Date: Sun, 12 Jan 2025 15:31:31 +0100 Subject: [PATCH 2/7] cleanup and add docs --- NEWS.md | 1 + README.md | 1 + inst/examples/conditional_app.R | 2 +- .../leaflet.layergroup.conditional-bindings.js | 18 +++++++----------- 4 files changed, 10 insertions(+), 12 deletions(-) diff --git a/NEWS.md b/NEWS.md index 463fe55..78e61de 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,6 +1,7 @@ # leaflet.extras2 (development version) * Included [LayerGroup.Collision](https://github.com/MazeMap/Leaflet.LayerGroup.Collision) plugin +* Included [LayerGroup.Conditional](https://github.com/Solfisk/Leaflet.LayerGroup.Conditional) plugin * Included [OSM Buildings](https://osmbuildings.org/documentation/leaflet/) plugin * New Function `addDivicon` adds `DivIcon` markers to Leaflet maps with support for custom HTML and CSS classes. See the example in `./inst/examples/divicons_html_app.R` * Added `addClusterCharts` to enable **pie** and **bar** charts in Marker clusters using `Leaflet.markercluster`, `d3` and `L.DivIcon`, with support for customizable category styling and various aggregation methods like **sum, min, max, mean**, and **median**. diff --git a/README.md b/README.md index 561bad7..1168449 100644 --- a/README.md +++ b/README.md @@ -42,6 +42,7 @@ If you need a plugin that is not already implemented create an [issue](https://g - [History](https://github.com/cscott530/leaflet-history) - [Labelgun](https://github.com/Geovation/labelgun) - [LayerGroup.Collision](https://github.com/MazeMap/Leaflet.LayerGroup.Collision) +- [LayerGroup.Conditional](https://github.com/Solfisk/Leaflet.LayerGroup.Conditional) - [Leaflet.Sync](https://github.com/jieter/Leaflet.Sync) - [Mapkey Icons](https://github.com/mapshakers/leaflet-mapkey-icon) - [Moving Markers](https://github.com/ewoken/Leaflet.MovingMarker) diff --git a/inst/examples/conditional_app.R b/inst/examples/conditional_app.R index bb1563f..07792c3 100644 --- a/inst/examples/conditional_app.R +++ b/inst/examples/conditional_app.R @@ -24,7 +24,7 @@ server <- function(input, output, session) { conditions = list( "(zoomLevel) => zoomLevel < 4" = groups[1], "(zoomLevel) => zoomLevel >= 4 & zoomLevel < 6 " = groups[2], - "(zoomLevel) => zoomLevel >= 6" = c(groups[1], groups[3]) + "(zoomLevel) => zoomLevel >= 6" = c(groups[3]) )) %>% hideGroup(groups) %>% addLayersControl(overlayGroups = groups, diff --git a/inst/htmlwidgets/lfx-conditional/leaflet.layergroup.conditional-bindings.js b/inst/htmlwidgets/lfx-conditional/leaflet.layergroup.conditional-bindings.js index 7c62921..acb610b 100644 --- a/inst/htmlwidgets/lfx-conditional/leaflet.layergroup.conditional-bindings.js +++ b/inst/htmlwidgets/lfx-conditional/leaflet.layergroup.conditional-bindings.js @@ -1,20 +1,19 @@ /* global LeafletWidget, $, L, Shiny, HTMLWidgets */ LeafletWidget.methods.addLayerGroupConditional = function(groups, conditions) { - var map = this; - var conditionalGroup = L.layerGroup.conditional(); - console.log("groups"); console.log(groups); console.log("conditions"); console.log(conditions) + const map = this; + let conditionalGroup = L.layerGroup.conditional(); // Loop through each group groups.forEach(function(group) { // Loop through conditions for each group Object.keys(conditions).forEach(function(condition) { - var groupList = conditions[condition]; + let groupList = conditions[condition]; if (!Array.isArray(groupList)) { groupList = [groupList]; } groupList.forEach(function(group) { - var layer = map.layerManager.getLayerGroup(group); + let layer = map.layerManager.getLayerGroup(group); if (!layer) { console.warn("Layer not found in group " + group); return; @@ -27,19 +26,16 @@ LeafletWidget.methods.addLayerGroupConditional = function(groups, conditions) { // Add the conditional group to the map conditionalGroup.addTo(map) - console.log("conditionalGroup");console.log(conditionalGroup); // Set up the zoom handler to update conditional layers - var zoomHandler = function() { - var zoomLevel = map.getZoom(); - console.log("zoomHandler: " + zoomLevel) - conditionalGroup.updateConditionalLayers(zoomLevel); + let zoomHandler = function() { + conditionalGroup.updateConditionalLayers(map.getZoom()); }; map.on("zoomend", zoomHandler); // Set initial state of conditional layers setTimeout(function() { zoomHandler() - }, 200); + }, 500); }; From 1b4fedfec79c6c88c6866ac21c84a7adb10596bc Mon Sep 17 00:00:00 2001 From: Sebastian Gatscha Date: Sun, 12 Jan 2025 16:17:28 +0100 Subject: [PATCH 3/7] feat: clear/remove conditional group --- NAMESPACE | 2 ++ R/layergroupconditional.R | 20 +++++++++++++++- inst/examples/conditional_app.R | 14 +++++++++-- ...leaflet.layergroup.conditional-bindings.js | 21 +++++++++++++++++ man/addLayerGroupConditional.Rd | 6 ++++- man/clearConditionalLayers.Rd | 20 ++++++++++++++++ man/removeConditionalLayer.Rd | 23 +++++++++++++++++++ 7 files changed, 102 insertions(+), 4 deletions(-) create mode 100644 man/clearConditionalLayers.Rd create mode 100644 man/removeConditionalLayer.Rd diff --git a/NAMESPACE b/NAMESPACE index fb8b91e..ce85abe 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -37,6 +37,7 @@ export(antpathOptions) export(arrowheadOptions) export(clearAntpath) export(clearArrowhead) +export(clearConditionalLayers) export(clearFuture) export(clearHexbin) export(clearHistory) @@ -75,6 +76,7 @@ export(playbackOptions) export(reachabilityOptions) export(removeAntpath) export(removeArrowhead) +export(removeConditionalLayer) export(removeEasyprint) export(removeItemContextmenu) export(removePlayback) diff --git a/R/layergroupconditional.R b/R/layergroupconditional.R index 5e6d77f..b4b7d17 100644 --- a/R/layergroupconditional.R +++ b/R/layergroupconditional.R @@ -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() %>% @@ -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") +} diff --git a/inst/examples/conditional_app.R b/inst/examples/conditional_app.R index 07792c3..544fe41 100644 --- a/inst/examples/conditional_app.R +++ b/inst/examples/conditional_app.R @@ -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) { @@ -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) diff --git a/inst/htmlwidgets/lfx-conditional/leaflet.layergroup.conditional-bindings.js b/inst/htmlwidgets/lfx-conditional/leaflet.layergroup.conditional-bindings.js index acb610b..993a042 100644 --- a/inst/htmlwidgets/lfx-conditional/leaflet.layergroup.conditional-bindings.js +++ b/inst/htmlwidgets/lfx-conditional/leaflet.layergroup.conditional-bindings.js @@ -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) { @@ -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() +}; diff --git a/man/addLayerGroupConditional.Rd b/man/addLayerGroupConditional.Rd index 0ce48b0..a46ed53 100644 --- a/man/addLayerGroupConditional.Rd +++ b/man/addLayerGroupConditional.Rd @@ -35,7 +35,7 @@ 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") leaflet() \%>\% addTiles() \%>\% @@ -56,5 +56,9 @@ addTiles() \%>\% } \seealso{ \url{https://github.com/Solfisk/Leaflet.LayerGroup.Conditional?tab=readme-ov-file} for more details about the plugin. + +Other LayerGroupConditional Plugin: +\code{\link{clearConditionalLayers}()}, +\code{\link{removeConditionalLayer}()} } \concept{LayerGroupConditional Plugin} diff --git a/man/clearConditionalLayers.Rd b/man/clearConditionalLayers.Rd new file mode 100644 index 0000000..c16a5a5 --- /dev/null +++ b/man/clearConditionalLayers.Rd @@ -0,0 +1,20 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/layergroupconditional.R +\name{clearConditionalLayers} +\alias{clearConditionalLayers} +\title{clearConditionalLayers} +\usage{ +clearConditionalLayers(map) +} +\arguments{ +\item{map}{A map widget object created from \code{\link[leaflet]{leaflet}}.} +} +\description{ +clearConditionalLayers +} +\seealso{ +Other LayerGroupConditional Plugin: +\code{\link{addLayerGroupConditional}()}, +\code{\link{removeConditionalLayer}()} +} +\concept{LayerGroupConditional Plugin} diff --git a/man/removeConditionalLayer.Rd b/man/removeConditionalLayer.Rd new file mode 100644 index 0000000..01b3ee2 --- /dev/null +++ b/man/removeConditionalLayer.Rd @@ -0,0 +1,23 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/layergroupconditional.R +\name{removeConditionalLayer} +\alias{removeConditionalLayer} +\title{removeConditionalLayer} +\usage{ +removeConditionalLayer(map, groups) +} +\arguments{ +\item{map}{A map widget object created from \code{\link[leaflet]{leaflet}}.} + +\item{groups}{A character vector of layer group names already added to the map. +These layer groups will be conditionally displayed based on the specified \code{conditions}.} +} +\description{ +removeConditionalLayer +} +\seealso{ +Other LayerGroupConditional Plugin: +\code{\link{addLayerGroupConditional}()}, +\code{\link{clearConditionalLayers}()} +} +\concept{LayerGroupConditional Plugin} From 525672a3d72df389271737c8c72d03f6b32f93f0 Mon Sep 17 00:00:00 2001 From: trafficonese Date: Sun, 12 Jan 2025 15:19:04 +0000 Subject: [PATCH 4/7] Style code (GHA) --- R/layergroupconditional.R | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/R/layergroupconditional.R b/R/layergroupconditional.R index b4b7d17..f87a5d7 100644 --- a/R/layergroupconditional.R +++ b/R/layergroupconditional.R @@ -40,23 +40,27 @@ layerGroupConditionalDependency <- function() { #' breweries91 <- st_as_sf(breweries91) #' lines <- st_as_sf(atlStorms2005) #' polys <- st_as_sf(leaflet::gadmCHE) -#' groups <- c("atlStorms","breweries","gadmCHE") +#' groups <- c("atlStorms", "breweries", "gadmCHE") #' -#' leaflet() %>% -#' addTiles() %>% +#' leaflet() %>% +#' addTiles() %>% #' leafem::addMouseCoordinates() %>% #' addPolylines(data = lines, label = ~Name, group = groups[1]) %>% #' addCircleMarkers(data = breweries91, label = ~brewery, group = groups[2]) %>% #' addPolygons(data = polys, label = ~NAME_1, group = groups[3]) %>% -#' addLayerGroupConditional(groups = groups, -#' conditions = list( -#' "(zoomLevel) => zoomLevel < 4" = groups[1], -#' "(zoomLevel) => zoomLevel >= 4 & zoomLevel < 6 " = groups[2], -#' "(zoomLevel) => zoomLevel >= 6" = groups[3] -#' )) %>% +#' addLayerGroupConditional( +#' groups = groups, +#' conditions = list( +#' "(zoomLevel) => zoomLevel < 4" = groups[1], +#' "(zoomLevel) => zoomLevel >= 4 & zoomLevel < 6 " = groups[2], +#' "(zoomLevel) => zoomLevel >= 6" = groups[3] +#' ) +#' ) %>% #' hideGroup(groups) %>% -#' addLayersControl(overlayGroups = groups, -#' options = layersControlOptions(collapsed=FALSE)) +#' addLayersControl( +#' overlayGroups = groups, +#' options = layersControlOptions(collapsed = FALSE) +#' ) #' addLayerGroupConditional <- function(map, groups = NULL, conditions = NULL) { map$dependencies <- c(map$dependencies, layerGroupConditionalDependency()) From 9891dc75dfdcc4a0da365580c266251cd777b56e Mon Sep 17 00:00:00 2001 From: Sebastian Gatscha Date: Sun, 12 Jan 2025 16:23:37 +0100 Subject: [PATCH 5/7] fix: comment out leafem --- R/layergroupconditional.R | 2 +- man/addLayerGroupConditional.Rd | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/R/layergroupconditional.R b/R/layergroupconditional.R index b4b7d17..bf77fdd 100644 --- a/R/layergroupconditional.R +++ b/R/layergroupconditional.R @@ -44,7 +44,7 @@ layerGroupConditionalDependency <- function() { #' #' leaflet() %>% #' addTiles() %>% -#' leafem::addMouseCoordinates() %>% +#' #leafem::addMouseCoordinates() %>% #' addPolylines(data = lines, label = ~Name, group = groups[1]) %>% #' addCircleMarkers(data = breweries91, label = ~brewery, group = groups[2]) %>% #' addPolygons(data = polys, label = ~NAME_1, group = groups[3]) %>% diff --git a/man/addLayerGroupConditional.Rd b/man/addLayerGroupConditional.Rd index a46ed53..1b61db7 100644 --- a/man/addLayerGroupConditional.Rd +++ b/man/addLayerGroupConditional.Rd @@ -39,7 +39,7 @@ groups <- c("atlStorms","breweries","gadmCHE") leaflet() \%>\% addTiles() \%>\% - leafem::addMouseCoordinates() \%>\% + #leafem::addMouseCoordinates() \%>\% addPolylines(data = lines, label = ~Name, group = groups[1]) \%>\% addCircleMarkers(data = breweries91, label = ~brewery, group = groups[2]) \%>\% addPolygons(data = polys, label = ~NAME_1, group = groups[3]) \%>\% From 7f03d6bacd332428048a05d716a77a2392aadf7e Mon Sep 17 00:00:00 2001 From: trafficonese Date: Sun, 12 Jan 2025 15:25:56 +0000 Subject: [PATCH 6/7] Update documentation --- man/addLayerGroupConditional.Rd | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/man/addLayerGroupConditional.Rd b/man/addLayerGroupConditional.Rd index 1b61db7..fc74aca 100644 --- a/man/addLayerGroupConditional.Rd +++ b/man/addLayerGroupConditional.Rd @@ -35,7 +35,7 @@ library(leaflet.extras2) breweries91 <- st_as_sf(breweries91) lines <- st_as_sf(atlStorms2005) polys <- st_as_sf(leaflet::gadmCHE) -groups <- c("atlStorms","breweries","gadmCHE") +groups <- c("atlStorms", "breweries", "gadmCHE") leaflet() \%>\% addTiles() \%>\% @@ -43,15 +43,19 @@ addTiles() \%>\% addPolylines(data = lines, label = ~Name, group = groups[1]) \%>\% addCircleMarkers(data = breweries91, label = ~brewery, group = groups[2]) \%>\% addPolygons(data = polys, label = ~NAME_1, group = groups[3]) \%>\% - addLayerGroupConditional(groups = groups, - conditions = list( - "(zoomLevel) => zoomLevel < 4" = groups[1], - "(zoomLevel) => zoomLevel >= 4 & zoomLevel < 6 " = groups[2], - "(zoomLevel) => zoomLevel >= 6" = groups[3] - )) \%>\% + addLayerGroupConditional( + groups = groups, + conditions = list( + "(zoomLevel) => zoomLevel < 4" = groups[1], + "(zoomLevel) => zoomLevel >= 4 & zoomLevel < 6 " = groups[2], + "(zoomLevel) => zoomLevel >= 6" = groups[3] + ) + ) \%>\% hideGroup(groups) \%>\% - addLayersControl(overlayGroups = groups, - options = layersControlOptions(collapsed=FALSE)) + addLayersControl( + overlayGroups = groups, + options = layersControlOptions(collapsed = FALSE) + ) } \seealso{ From d681c79a05bda0f283bc9b32e03b65bd6059d969 Mon Sep 17 00:00:00 2001 From: Sebastian Gatscha Date: Sun, 12 Jan 2025 16:28:50 +0100 Subject: [PATCH 7/7] fix: add pkgdown --- _pkgdown.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/_pkgdown.yml b/_pkgdown.yml index 10fe31f..db01067 100644 --- a/_pkgdown.yml +++ b/_pkgdown.yml @@ -62,6 +62,10 @@ reference: - title: LayerGroup.Collision contents: - matches("LayerGroupCollision") + - title: LayerGroup.Conditional + contents: + - matches("LayerGroupConditional") + - matches("ConditionalLayer") - title: Mapkey Icons contents: - matches("Mapkey")