I would like to use the leaflet easyButton plugin to control the layers shown instead of the built in layer controls. Unfortunately I do not know JS, and have spent several hours trying to make sense of the leafletJS documentation to find the correct way to do this.
Here is a slimmed down version of the code I'm attempting. The onClick is clearly working since the alerts happen, but nothing changes with the layer being shown.
Each button should show ONLY that one layer and none of the others (as opposed to allowing multiple layers to be seen at once).
library(tidyverse)
library(sf)
library(tigris)
library(leaflet)
statemap <- states(cb = T)
statemap <- statemap %>%
filter(GEOID < 60) %>%
shift_geometry(geoid_column = "GEOID") %>%
st_transform("EPSG:4326")
leaflet(statemap) %>%
addPolygons(weight = 1.25, color = "#000000", opacity = 1,#444444
fillColor = "green",
layerId = "green") %>%
addPolygons(weight = 1.25, color = "#000000", opacity = 1,#444444
fillColor = "red",
layerId = "red") %>%
addPolygons(weight = 1.25, color = "#000000", opacity = 1,#444444
fillColor = "yellow",
layerId = "yellow") %>%
addEasyButtonBar(easyButton(icon = "fa-globe", title = "Drinking Water Standard",
onClick = JS("function(btn, map) {
alert(\"Green\");
map.clearLayers();
map.addLayer('green');
}")),
easyButton(icon = "fa-crosshairs", title = "Groundwater Standard",
onClick = JS("function(btn, map) {
alert(\"Yellow\");
map.clearLayers();
map.addLayer('yellow');
}")),
easyButton(icon = "fa-globe", title = "Surface Water Standard",
onClick = JS("function(btn, map) {
alert(\"Red\");
map.clearLayers();
map.addLayer('red');
}")))