Open warnes opened 1 year ago
I've found a workaround using shinyjs::runjs
for this simple example. Modifying the filteredData
observer to:
# Incremental changes to the map (in this case, replacing the
# circles when a new color is chosen) should be performed in
# an observer. Each independent set of things that can change
# should be managed in its own observer.
observe({
pal <- colorpal()
sized_data <- filteredData
##-- Changes start here --##
proxy <- leafletProxy("map", data = filteredData()) %>%
clearShapes() %>%
addCircles(radius = ~10^mag/10, weight = 1, color = "#777777",
fillColor = ~pal(mag), fillOpacity = 0.7, popup = ~paste(mag)
) %>%
# removeEasyButton(id = "zoom-to-filtered") %>% ## Wish: remove the previous easyButton ##
addEasyButton(easyButton(
icon="fa-rotate-right",
title = "Zoom to filtered circles",
id = "zoom-to-filtered",
onClick = JS(
glue(
"
function(btn, map) {{
var maxBounds = L.latLngBounds(
L.latLng({min(filteredData()$lat)}, {min(filteredData()$long)}), //Southwest
L.latLng({max(filteredData()$lat)}, {max(filteredData()$long)}) //Northeast
);
map.fitBounds(maxBounds);
}}
"
)
)
))
shinyjs::runjs("
var zoomToFiltered = document.getElementById('zoom-to-filtered');
zoomToFiltered.parentNode.removeChild(zoomToFiltered);
")
proxy
##-- Changes end here --##
})
This isn't really satisfactory since it relies on document.getElementById('zoom-to-filtered')
returning the earliest defined element with the id 'zoom-to-filtered', and I'm not sure this is guaranteed.
I have a
shiny
app that usesleaflet
, and I am using 'addEasyButton' to add a button that allows the user to restore the bounds that I set programmatically.Unfortunately, there doesn't appear to be a way to remove or update a button created using
addEasyButton
, making it difficult to update the action that is triggered when the button is pressed.The following code is a lightly modified version of the second example on https://rstudio.github.io/leaflet/shiny.html
It adds a call to addEasyButton that adds a button that changes the map bounds to enclose only the earthquakes that match the magnitude filter.
The problem is that each time the filters is changed, a new button is added to the map, and there doesn't appear to be any way to remove or update the previously created buttons, so the buttons accumulate (even though they have the same id):
If the call
removeEasyButton
was present, it could be added before theaddEasyButton
call to remove the old one.Here is the map after changing the magnutitude slider 3 times: