Search code examples
rshinyr-leaflet

Change easyButton State Within Server Function in R Shiny App


Does anyone know of a way to change the state of an easyButton inside the server function of an R Shiny app?

leaflet() %>%
    setView(lng = -70.4803, lat = 40.94404, zoom = 6) %>%
    addProviderTiles('Esri.WorldImagery') %>%
    addEasyButton(
    easyButton(id = 'edit-btn', states = list(
      easyButtonState( 
        stateName = 'add-toolbar',
        icon = icon('toggle-off'),
        title = 'Edit',
        onClick = JS("
          function(btn, map) {
            Shiny.onInputChange('edit_btn', 'TRUE');
            btn.state('remove-toolbar');
          }"
        )
      ),
      easyButtonState(
        stateName = 'remove-toolbar',
        icon = icon('toggle-on'),
        title = 'Editing',
        onClick = JS("
          function(btn, map) {
            Shiny.onInputChange('edit_btn', 'FALSE');
            btn.state('add-toolbar');
          }"
        )
      ))
    )
)

Then I tried to change the state of this easyButton within a server function that triggers when another action button is clicked:

runjs("edit-btn.state('add-toolbar');")

But this doesn't seem to work.


Solution

  • You can do this with the shinyjs click() function. Here is a reproducible example:

    library(shiny)
    library(shinyjs)
    ui <- fluidPage(
      useShinyjs(),
      actionButton("toggle_button","My Button"),
      actionButton("disable_button","Toggle State"),
      leafletOutput("my_map")
    )
    
    server <- function(input, output, session) {
      
      observeEvent(input$toggle_button,{
        shinyjs::click("edit-btn")
      })
      
      observeEvent(input$disable_button,({
        shinyjs::toggleState("edit-btn")
      }))
      
      output$my_map <- renderLeaflet({
        leaflet() %>%
          setView(lng = -70.4803, lat = 40.94404, zoom = 6) %>%
          addProviderTiles('Esri.WorldImagery') %>%
          addEasyButton(
            easyButton(id = 'edit-btn', 
                       states = list(
                         easyButtonState( 
                           stateName = 'add-toolbar',
                           icon = icon('toggle-off'),
                           title = 'Edit',
                           onClick = JS("
              function(btn, map) {
                Shiny.onInputChange('edit_btn', 'TRUE');
                btn.state('remove-toolbar');
              }"
                           )
                         ),
                         easyButtonState(
                           stateName = 'remove-toolbar',
                           icon = icon('toggle-on'),
                           title = 'Editing',
                           onClick = JS("
              function(btn, map) {
                Shiny.onInputChange('edit_btn', 'FALSE');
                btn.state('add-toolbar');
              }"
                           )
                         ))
            )
          )
      })
    }
    
    shinyApp(ui, server)