简体   繁体   English

在 shiny 模块中使用 updateMaterialSwitch 的观察事件未更新输入

[英]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.我想使用 observeEvent 更新 shiny 模块中的 materialSwitch,触发事件但 updateMaterialSwitch 不会更改输入值。 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.当 observeEvents 在 app_server 中时,一切正常。 When I move them to mod_1_server, events are triggered but expected value of input$uncumulate_tagvals (my inputId) is never changed.当我将它们移动到 mod_1_server 时,会触发事件,但 input$uncumulate_tagvals (我的 inputId)的预期值永远不会改变。 My guess is that problem could be with session, but I don`t know how to solve it.我的猜测是问题可能出在 session 上,但我不知道如何解决。 Any suggestions?有什么建议么?

I think the problem comes from the lack of namespace specification, with the use of session$ns()我认为问题来自缺少命名空间规范,使用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/如果您需要有关如何转换为模块的更多信息,可以阅读此博客文章: https://rtask.thinkr.fr/communication-between-modules-and-its-whims/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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