Skip to content

toggle_switch() doesn't work in a Shiny module #769

Closed
@gadenbuie

Description

@gadenbuie
library(shiny)
library(bslib)

ui_switch <- function(id) {
  ns <- NS(id)
  tagList(
    input_switch(ns("switch_toggle"), label = "Follow Switch (toggle)", value = TRUE),
    input_switch(ns("switch_update"), label = "Follow Switch (update)", value = TRUE)
  )
}

server_switch <- function(id, value) {
  shiny::moduleServer(id, function(input, output, session, ...) {
    observeEvent(value(), {
      toggle_switch("switch_toggle", value = value())
      update_switch("switch_update", value = value())
    })
  })
}

ui <- page_fixed(
  input_switch("leader", "Leader Switch", value = FALSE),
  ui_switch("follower")
)

server <- function(input, output, session) {
  server_switch("follower", reactive(input$leader))
}

shinyApp(ui, server)

The above app should show three toggle switches. When the leader switch is toggled, the follower switches (in the shiny module) should also update. Only the one called via update_switch() successfully changes the switch state, because that call goes through session$sendInputMessage() rather than session$sendCustomMessage().

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions