简体   繁体   中英

observeEvent with updateMaterialSwitch in shiny module not updating input

I want to update my materialSwitch inside shiny module with observeEvent, event is triggered but updateMaterialSwitch doesn`t change input value. My code snippet:

# app server
app_server <- function(input, output, session) {
  r <- reactiveValues()
  observe(r$is_load <- is_load()) # basic reactive true/false - switching according to condition
  callModule(mod_1_server, "1", r = r)
}

# mod_1_server
mod_1_server <- function(input, output, session, r) {

  output$switch_uncumulate_tagvals <- renderUI({
    materialSwitch(
      inputId = "uncumulate_tagvals",
      label = "label",
      value = FALSE,
      status = "warning"
    )
  })

  observeEvent(req(r$is_load() == TRUE), {
    updateMaterialSwitch(session = session,
                         inputId = "uncumulate_tagvals",
                         value = TRUE)
  })

  observeEvent(req(r$is_load() == FALSE), {
    updateMaterialSwitch(session = session,
                         inputId = "uncumulate_tagvals",
                         value = FALSE)
  })

}

When observeEvents are in app_server, everything is working. When I move them to mod_1_server, events are triggered but expected value of input$uncumulate_tagvals (my inputId) is never changed. My guess is that problem could be with session, but I don`t know how to solve it. Any suggestions?

I think the problem comes from the lack of namespace specification, with the use of session$ns()

# mod_1_server
mod_1_server <- function(input, output, session, r) {

  # namespace fonction
  ns <- session$ns

  output$switch_uncumulate_tagvals <- renderUI({
    materialSwitch(
      inputId = ns("uncumulate_tagvals"),
      label = "label",
      value = FALSE,
      status = "warning"
    )
  })

  observeEvent(req(r$is_load() == TRUE), {
    updateMaterialSwitch(session = session,
                         inputId = "uncumulate_tagvals",
                         value = TRUE)
  })

  observeEvent(req(r$is_load() == FALSE), {
    updateMaterialSwitch(session = session,
                         inputId = "uncumulate_tagvals",
                         value = FALSE)
  })

}

If you need more information on how to transform as module, you can read this blog post: https://rtask.thinkr.fr/communication-between-modules-and-its-whims/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM