Not straight forward, but possible.
Using this doc, https://shiny.rstudio.com/articles/js-send-message.html, and the onClick
param for the easy button, you can set a shiny input value from within the on-click method. In the example below, I set my_easy_button
to either "frozen"
or "free"
.
Let me know how this works out for you!
library(shiny)
library(leaflet)
r_colors <- rgb(t(col2rgb(colors()) / 255))
names(r_colors) <- colors()
ui <- fluidPage(
leafletOutput("mymap")
)
server <- function(input, output, session) {
observeEvent(input$my_easy_button, {
str(input$my_easy_button)
})
output$mymap <- renderLeaflet({
leaflet() %>%
addTiles() %>%
addMarkers(data=quakes,
clusterOptions = markerClusterOptions(),
clusterId = "quakesCluster") %>%
addEasyButton(easyButton(
states = list(
easyButtonState(
stateName="unfrozen-markers",
icon="ion-toggle",
title="Freeze Clusters",
onClick = JS("
function(btn, map) {
var clusterManager =
map.layerManager.getLayer('cluster', 'quakesCluster');
clusterManager.freezeAtZoom();
btn.state('frozen-markers');
Shiny.onInputChange('my_easy_button', 'frozen');
}")
),
easyButtonState(
stateName="frozen-markers",
icon="ion-toggle-filled",
title="UnFreeze Clusters",
onClick = JS("
function(btn, map) {
var clusterManager =
map.layerManager.getLayer('cluster', 'quakesCluster');
clusterManager.unfreeze();
btn.state('unfrozen-markers');
Shiny.onInputChange('my_easy_button', 'free');
}")
)
)
))
})
}
shinyApp(ui, server)
