簡體   English   中英

如何從另一個閃亮模塊更新閃亮模塊中的輸入?

[英]How to update input in shiny modlue from another shiny module?

我有兩個閃亮的模塊,一個是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)

您可以通過使第一個 input$update 反應,然后返回該值並使其反應到第二個服務器模塊來做到這一點。 這樣,第二個服務器模塊就“監聽”了第一個服務器模塊的變化。

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 = "")
    })
    reactive(input$update)
  })
}
secondUI <- function(id) {
  ns <- NS(id)
  tagList(
    textInput(ns("second"), "Update me pls", value = "Clear me!")
  )
}
secondServer <- function(id, clear) {
  moduleServer(id, function(input, output, session) {
    observeEvent(clear(), {
      updateTextInput(session = session, "first", value = "")
      updateTextInput(session = session,"second", value = "")
    })
  })
}

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

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

shinyApp(ui, server)

一種迂回的方法是使用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)

閃亮的模塊通過將[module_name]-[element_id]一起粘貼到 html 前端來為每個元素提供唯一的 id,因此每個模塊服務器都可以正確識別它應該與哪個模塊通信。 當直接傳遞該 id 時,第一台服務器可以找到並與module_two-second對話。 理想情況下,可能有一種方法可以在 Shiny 代碼本身中執行此操作。

編輯:通過傳遞 parent_session 在 Shiny 中修復(沒有shinyjs

如果updateTextInput調用可以查看自己的會話環境之外,它確實可以找到module_two-second本身。 為此,您可以將parent_session作為參數傳遞給updateTextInput (在firstServer函數定義中定義並在server主體中作為parent_session = session傳遞):

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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM