Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
gadenbuie opened this issue Aug 29, 2023 · 1 comment · Fixed by #770
Closed

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

gadenbuie opened this issue Aug 29, 2023 · 1 comment · Fixed by #770
Assignees

Comments

@gadenbuie
Copy link
Member

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().

@github-actions
Copy link

This issue has been automatically locked. If you have found a related problem, please open a new issue (with a reproducible example or feature request) and link to this issue.
🙋 Need help? Connect with us on Discord or Posit Community.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 30, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant