如何从另一个闪亮模块更新闪亮模块中的输入?

pol*_*pik 6 r shiny

我有两个闪亮的模块,updateTextInput()一个是。当单击第一个模块的按钮时,我想textInput()第二个模块中进行更新。我知道这是因为这些模块位于不同的命名空间中,但我不知道如何通信模块。

代表如下:)

library(shiny)

firstUI <- function(id) {
  ns <- NS(id)
  tagList(
    actionButton(ns("update"), "Update 1st and 2nd module"),
    textInput(ns("first"), "Update me pls1", value = "Clear me!")
    
  )
}
firstServer <- function(id) {
  moduleServer(id, function(input, output, session) {
    observeEvent(input$update, {
      updateTextInput(session = session, "first", value = "")
      updateTextInput(session = session,"second", value = "")
    })
})
}
secondUI <- function(id) {
  ns <- NS(id)
  tagList(
    textInput(ns("second"), "Update me pls", value = "Clear me!")
  )
}
secondServer <- function(id) {
  moduleServer(id, function(input, output, session) {
    observeEvent(input$update, {
      updateTextInput(session = session, "first", value = "")
      updateTextInput(session = session,"second", value = "")
    })
})
}

ui <- fluidPage(
  firstUI("module_one"),
  secondUI("module_two")
)

server <- function(input, output, session) {
  firstServer("module_one")
  secondServer("module_two")
}

shinyApp(ui, server)
Run Code Online (Sandbox Code Playgroud)

And*_*ter 3

一种迂回的方法是使用shinyjs手动触发更新。

library(shiny)
library(shinyjs)

firstUI <- function(id) {
  ns <- NS(id)
  tagList(
    actionButton(ns("update"), "Update 1st and 2nd module"),
    textInput(ns("first"), "Update me pls1", value = "Clear me!")
    
  )
}
firstServer <- function(id) {
  moduleServer(id, function(input, output, session) {
    observeEvent(input$update, {
      updateTextInput(session = session, "first", value = "")
      runjs('document.getElementById("module_two-second").value = ""')
    })
  })
}

secondUI <- function(id) {
  ns <- NS(id)
  tagList(
    textInput(ns("second"), "Update me pls", value = "Clear me!")
  )
}

secondServer <- function(id) {
  moduleServer(id, function(input, output, session) {
    # Code not needed in here for now
  })
}

ui <- fluidPage(
  useShinyjs(),
  firstUI("module_one"),
  secondUI("module_two")
)

server <- function(input, output, session) {
  firstServer("module_one")
  secondServer("module_two")
}

shinyApp(ui, server)
Run Code Online (Sandbox Code Playgroud)

Shiny 模块的工作原理是在 html 前端粘贴在一起,为每个元素提供一个唯一的 ID [module_name]-[element_id],这样每个模块服务器就可以正确识别它应该与之通信的模块。当直接传递该 id 时,第一个服务器可以找到并与之对话module_two-second。理想情况下,在 Shiny 代码本身中可能有一种方法可以做到这一点。

编辑:通过传递parent_session(不带shinyjs)在Shiny中修复

如果调用可以在自己的会话环境之外进行查找,那么它updateTextInput确实可以找到自己。module_two-second要实现此目的,您可以将parent_session作为参数传递给updateTextInput(在函数定义中定义并在主体中firstServer传递):parent_session = sessionserver

library(shiny)

firstUI <- function(id) {
  ns <- NS(id)
  tagList(
    actionButton(ns("update"), "Update 1st and 2nd module"),
    textInput(ns("first"), "Update me pls1", value = "Clear me!")
    
  )
}
firstServer <- function(id, parent_session) {
  moduleServer(id, function(input, output, session) {
    observeEvent(input$update, {
      updateTextInput(session = session, "first", value = "")
      updateTextInput(session = parent_session, "module_two-second", value = "")
    })
  })
}

secondUI <- function(id) {
  ns <- NS(id)
  tagList(
    textInput(ns("second"), "Update me pls", value = "Clear me!")
  )
}

secondServer <- function(id) {
  moduleServer(id, function(input, output, session) {
    # Code not needed in here for now
  })
}

ui <- fluidPage(
  firstUI("module_one"),
  secondUI("module_two")
)

server <- function(input, output, session) {
  firstServer("module_one", parent_session = session)
  secondServer("module_two")
}

shinyApp(ui, server)
Run Code Online (Sandbox Code Playgroud)