/
showhide.Rmd
144 lines (110 loc) · 5.48 KB
/
showhide.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
---
title: "Show/Hide Layers"
---
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>"
)
```
```{r,echo=FALSE}
library(leaflet)
library(magrittr)
```
The Leaflet package includes functions to show and hide map layers. You can allow users to decide what layers to show and hide, or programmatically control the visibility of layers using [server-side code in Shiny](https://rstudio.github.io/leaflet/articles/shiny.html).
In both cases, the fundamental unit of showing/hiding is the **group**.
# Understanding Groups
A group is a label given to a set of layers. You assign layers to groups by using the `group` parameter when adding the layers to the map.
```r
leaflet() %>%
addTiles() %>%
addMarkers(data = coffee_shops, group = "Food & Drink") %>%
addMarkers(data = restaurants, group = "Food & Drink") %>%
addMarkers(data = restrooms, group = "Restrooms")
```
Many layers can belong to same group. But each layer can only belong to zero or one groups (you can't assign a layer to two groups).
<div class="panel panel-info">
<div class="panel-heading">
<h4 class="panel-title">Groups vs. Layer IDs</h4>
</div>
<div class="panel-body">
Groups and [Layer IDs](https://rstudio.github.io/leaflet/articles/shiny.html#understanding-layer-ids) may appear similar, in that both are used to assign a name to a layer. However, they differ in that layer IDs are used to provide a *unique identifier* to *individual* markers and shapes, etc., while groups are used to give *shared labels* to *many* items.
You generally provide one `group` value for the entire `addMarkers()` call, and you can reuse that same `group` value in future `add*()` calls to add to that group's membership (as in the example above).
`layerId` arguments are *always* vectorized: when calling e.g. `addMarkers()` you need to provide one layer ID per marker, and they must all be unique. If you add a circle with a layer ID of `"foo"` and later add a different shape with the same layer ID, the original circle will be removed.
</div>
</div>
# Interactive Layer Display
You can use Leaflet's layers control feature to allow users to toggle the visibility of groups.
```{r fig.height=3}
outline <- quakes[chull(quakes$long, quakes$lat),]
map <- leaflet(quakes) %>%
# Base groups
addTiles(group = "OSM (default)") %>%
addProviderTiles(providers$CartoDB.Positron, group = "Positron (minimal)") %>%
addProviderTiles(providers$Esri.WorldImagery, group = "World Imagery (satellite)") %>%
# Overlay groups
addCircles(
~ long,
~ lat,
~ 10 ^ mag / 5,
stroke = FALSE,
group = "Quakes",
fillColor = "tomato"
) %>%
addPolygons(
data = outline,
lng = ~ long,
lat = ~ lat,
fill = FALSE,
weight = 2,
color = "#FFFFCC",
group = "Outline"
) %>%
# Layers control
addLayersControl(
baseGroups = c(
"OSM (default)",
"Positron (minimal)",
"World Imagery (satellite)"
),
overlayGroups = c("Quakes", "Outline"),
options = layersControlOptions(collapsed = FALSE)
)
map
```
`addLayersControl()` distinguishes between *base groups*, which can only be viewed one group at a time, and *overlay groups*, which can be individually checked or unchecked.
Although base groups are generally tile layers, and overlay groups are usually markers and shapes, there is no restriction on what types of layers can be placed in each category.
Only one layers control can be present on a map at a time. If you call `addLayersControl()` multiple times, the last call will win.
# Programmatic Layer Display
You can use `showGroup()` and `hideGroup()` to show and hide groups from code. This mostly makes sense in a [Shiny context with `leafletProxy`](https://rstudio.github.io/leaflet/articles/shiny.html), where perhaps you might toggle group visibility based on input controls in a sidebar.
You can also use `showGroup()`/`hideGroup()` in conjunction with `addLayersControl()` to set which groups are checked by default.
```{r}
map %>% hideGroup("Outline")
```
Finally, you can remove the layers in a group using `clearGroup()`. Note that this doesn't just remove the layers from the group, it also removes them from the map. (It does not, however, remove the group itself from the map; it still exists, but is empty.)
# With Marker Clusters
If markers are added to different groups, and when using marker clustering as described in the [marker](https://rstudio.github.io/leaflet/articles/markers.html) page, leaflet will generate different sets of clusters for different groups. This allows showing/hiding of marker clusters belonging to a group independently of other marker clusters in other groups.
```{r}
quakes <- quakes %>%
dplyr::mutate(mag.level = cut(mag,c(3,4,5,6),
labels = c('>3 & <=4', '>4 & <=5', '>5 & <=6')))
quakes.df <- split(quakes, quakes$mag.level)
l <- leaflet() %>% addTiles()
names(quakes.df) %>%
purrr::walk( function(df) {
l <<- l %>%
addMarkers(data=quakes.df[[df]],
lng=~long, lat=~lat,
label=~as.character(mag),
popup=~as.character(mag),
group = df,
clusterOptions = markerClusterOptions(removeOutsideVisibleBounds = FALSE),
labelOptions = labelOptions(noHide = FALSE,
direction = 'auto'))
})
l %>%
addLayersControl(
overlayGroups = names(quakes.df),
options = layersControlOptions(collapsed = FALSE)
)
```