Although I can use a Java Script workaround to produce the r-leaflet result I want in the RStudio viewer, I do not get the same expected result when I knit to HTML. I'm not sure if it's an RMarkdown issue or a leaflet issue or both.
In general I'm looking for a more robust way to make R leaflet legends appear and disappear depending on which group layer is clicked (I do not want the layers to be overlays). The answer to this stackoverflow question is a game-changer and it works very well for a single map. I tried to apply this JS solution to my own function that takes a table and makes it into a leaflet map. In my real case, such a function would save me from a copying and pasting dozens of times, and would allow the script to be hundreds of lines shorter.
The output of my function displays as expected in an RStudio viewer, with the ability to toggle the layers with corresponding legends. However when I knit to HTML, the function does not work if I use it more than once. Specifically the legends do not work as expected. I want to put several maps, text, and tables together in nice document, but I'm not committed to HTML.
I see from this leaflet issue that people would like this feature in leaflet but it has not been implemented, and I wasn't able to apply the workarounds suggested there.
See mini example below:
---
title: "test"
output: html_document
date: "2023-06-09"
---
```{r}
library(leaflet)
cities1 <- data.frame(City = factor(c("Boston", "Hartford",
"New York City", "Philadelphia", "Pittsburgh", "Providence")),
Lat = c(42.3601, 41.7627, 40.7127, 39.95, 40.4397, 41.8236),
Long = c(-71.0589, -72.6743, -74.0059, -75.1667, -79.9764, -71.4222),
Pop = c(645966L, 125017L, 8406000L, 1553000L, 305841L, 177994L),
Type = factor(c("C", "D", "A", "A", "B", "C")),
supply = c("small", "large", "small", "large", "medium", "large"))
cities2 <- data.frame(City = factor(c("Baltimore", "Ithaca", "Wareham")),
Lat = c(39.299236, 42.443962, 41.761452),
Long = c(-76.609383, -76.501884, -70.719734),
Pop = c(609032L, 30569L, 22666L),
Type = factor(letters[1:3]),
supply = factor(c("small", "small", "medium")))
map_city <- function(city_grp){
pal1 <- colorFactor("viridis", domain = city_grp$Type)
pal2 <- colorFactor("Set1", domain = city_grp$supply)
my_map <- leaflet(city_grp) %>%
addTiles() %>%
addCircles(data = city_grp, lng = city_grp$Long, lat = city_grp$Lat, weight = 1, group="one",
radius = sqrt(city_grp$Pop) * 30, popup = city_grp$City, color = pal1(city_grp$Type), opacity = .9
) %>%
addLegend(pal = pal1, values = city_grp$Type, group = "one", layerId = "one") %>%
addCircles(data = city_grp, lng = city_grp$Long, lat = city_grp$Lat, weight = 1, group = "two",
radius = sqrt(city_grp$Pop) * 30, popup = city_grp$City, color = pal2(city_grp$supply), opacity = .9
) %>%
addLegend(pal = pal2, values = city_grp$supply, data = city_grp, group = "two", layerId = "two") %>%
addLayersControl(
baseGroups = c("one", "two"),
options = layersControlOptions(collapsed = FALSE),
position = "topleft"
) %>%
htmlwidgets::onRender("
function() {
var map = this;
var legends = map.controls._controlsById;
function addActualLegend() {
var sel = $('.leaflet-control-layers-base').find('input[type=\"radio\"]:checked').siblings('span').text().trim();
$.each(map.controls._controlsById, (nm) => map.removeControl(map.controls.get(nm)));
map.addControl(legends[sel]);
}
$('.leaflet-control-layers-base').on('click', addActualLegend);
addActualLegend();
}")
return(my_map)
}
map_city(cities1)
map_city(cities2)
```
> sessionInfo()
R version 4.2.2 (2022-10-31 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19045)
Matrix products: default
locale:
[1] LC_COLLATE=English_United States.utf8 LC_CTYPE=English_United States.utf8 LC_MONETARY=English_United States.utf8 LC_NUMERIC=C
[5] LC_TIME=English_United States.utf8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] leaflet_2.1.2.9000
loaded via a namespace (and not attached):
[1] compiler_4.2.2 pillar_1.9.0 RColorBrewer_1.1-3 jquerylib_0.1.4 viridis_0.6.3 tools_4.2.2 digest_0.6.31 jsonlite_1.8.5 evaluate_0.21
[10] lifecycle_1.0.3 tibble_3.2.1 gtable_0.3.3 viridisLite_0.4.2 pkgconfig_2.0.3 rlang_1.1.1 cli_3.6.0 rstudioapi_0.14 crosstalk_1.2.0
[19] yaml_2.3.7 xfun_0.39 fastmap_1.1.0 gridExtra_2.3 dplyr_1.1.2 knitr_1.43 generics_0.1.3 htmlwidgets_1.6.2 vctrs_0.6.2
[28] grid_4.2.2 tidyselect_1.2.0 glue_1.6.2 R6_2.5.1 fansi_1.0.4 rmarkdown_2.22 ggplot2_3.4.2 farver_2.1.1 magrittr_2.0.3
[37] ellipsis_0.3.2 scales_1.2.1 htmltools_0.5.4 colorspace_2.1-0 utf8_1.2.3 munsell_0.5.0