jzadra
jzadra

Reputation: 4304

R-leaflet - addControl() for specific layer only

I'm using leaflet::addControl() to add an information box to my 3 layered map, but only want it to appear on specific layer(s). By default, it appears on every layer, and doesn't seem to have any arguments to specify a specific layer or layers.

How can I accomplish this?

Working example:

library(tidyverse)
library(leaflet)
#library(leaflet.extras)
#library(leafem)
library(sf)
library(tigris)


statemap <- states(cb = T, resolution = "20m")

statemap <- statemap %>% 
  filter(GEOID < 60) %>% 
  st_transform("EPSG:4326")


leaflet() %>% 
  addPolygons(data = statemap,  weight = 0.5, color = "#000000", opacity = 1,
              fillColor = "red", fillOpacity = 0.7, smoothFactor = 0.5,
              group = "red") %>% 
  addPolygons(data = statemap,  weight = 0.5, color = "#000000", opacity = 1,
              fillColor = "yellow", fillOpacity = 0.7, smoothFactor = 0.5,
              group = "yellow") %>% 
  addPolygons(data = statemap,  weight = 0.5, color = "#000000", opacity = 1,
              fillColor = "blue", fillOpacity = 0.7, smoothFactor = 0.5,
              group = "blue") %>% 
  addLayersControl(baseGroups = c("red", "yellow", "blue"),
                   options=layersControlOptions(collapsed = F)) %>% #This is here temporarily, the buttons on top of the map control the layer displayed.
  addControl("This should only show on RED", position = "bottomright") %>% 
  setView(zoom = 3.5, lat =38, lng = -100)

Upvotes: 1

Views: 363

Answers (1)

keyspeler Noctum
keyspeler Noctum

Reputation: 11

add:

%>% 
    hideGroup(c("red", "yellow", "blue"))

or only the group(s) that you want to show

Upvotes: 0

Related Questions