闪亮模块中带有updateMaterialSwitch的watchEvent不会更新输入

时间:2020-05-25 17:02:11

标签: r shiny shinywidgets shinymodules

我想使用observeEvent更新闪亮模块内部的materialSwitch,触发事件,但updateMaterialSwitch不会更改输入值。我的代码段:

# 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)
  })

}

当watchEvents在app_server中时,一切正常。当我将它们移至mod_1_server时,将触发事件,但input $ uncumulate_tagvals(我的inputId)的期望值从未更改。我的猜测是会话可能存在问题,但我不知道该如何解决。有什么建议吗?

1 个答案:

答案 0 :(得分:1)

我认为问题出在使用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)
  })

}

如果您需要有关如何转换为模块的更多信息,可以阅读以下博客文章:https://rtask.thinkr.fr/communication-between-modules-and-its-whims/