模块中使用的 observeEvent Shiny 函数不起作用
observeEvent Shiny function used in a module does not work
我正在开发一个应用程序,我在其中使用模块来显示不同选项卡的 ui 内容。但是,该模块似乎不与主(或父)应用程序通信。它显示正确的 ui 但在单击 actionButton
时无法执行 observeEvent
函数,它应该更新当前选项卡并显示第二个选项卡。
在我的代码中,我创建了一个命名空间函数并将 actionButton
的 ID 包装在 ns()
中,但它仍然不起作用。有谁知道怎么了?
library(shiny)
moduleUI <- function(id){
ns <- NS(id)
sidebarPanel(
actionButton(ns("action1"), label = "click")
)
}
module <- function(input, output, session){
observeEvent(input$action1, {
updateTabItems(session, "tabsPanel", "two")
})
}
ui <- fluidPage(
navlistPanel(id = "tabsPanel",
tabPanel("one",moduleUI("first")),
tabPanel("two",moduleUI("second"))
))
server <- function(input, output, session){
callModule(module,"first")
callModule(module,"second")
}
shinyApp(ui = ui, server = server)
observeEvent 有效,但由于模块只能看到和知道作为输入参数提供给它们的变量,它不知道指定的 tabsetPanel,因此无法更新它。这个问题可以使用反应值来解决,它作为参数传递并在模块内部更改。一旦更改,主应用程序就会知道它并可以更新 tabsetPanel:
library(shiny)
library(shinydashboard)
moduleUI <- function(id){
ns <- NS(id)
sidebarPanel(
actionButton(ns("action1"), label = "click")
)
}
module <- function(input, output, session, tabsPanel, openTab){
observeEvent(input$action1, {
if(tabsPanel() == "one"){ # input$tabsPanel == "one"
openTab("two")
}else{ # input$tabsPanel == "two"
openTab("one")
}
})
return(openTab)
}
ui <- fluidPage(
h2("Currently open Tab:"),
verbatimTextOutput("opentab"),
navlistPanel(id = "tabsPanel",
tabPanel("one", moduleUI("first")),
tabPanel("two", moduleUI("second"))
))
server <- function(input, output, session){
openTab <- reactiveVal()
observe({ openTab(input$tabsPanel) }) # always write the currently open tab into openTab()
# print the currently open tab
output$opentab <- renderPrint({
openTab()
})
openTab <- callModule(module,"first", reactive({ input$tabsPanel }), openTab)
openTab <- callModule(module,"second", reactive({ input$tabsPanel }), openTab)
observeEvent(openTab(), {
updateTabItems(session, "tabsPanel", openTab())
})
}
shinyApp(ui = ui, server = server)
我正在开发一个应用程序,我在其中使用模块来显示不同选项卡的 ui 内容。但是,该模块似乎不与主(或父)应用程序通信。它显示正确的 ui 但在单击 actionButton
时无法执行 observeEvent
函数,它应该更新当前选项卡并显示第二个选项卡。
在我的代码中,我创建了一个命名空间函数并将 actionButton
的 ID 包装在 ns()
中,但它仍然不起作用。有谁知道怎么了?
library(shiny)
moduleUI <- function(id){
ns <- NS(id)
sidebarPanel(
actionButton(ns("action1"), label = "click")
)
}
module <- function(input, output, session){
observeEvent(input$action1, {
updateTabItems(session, "tabsPanel", "two")
})
}
ui <- fluidPage(
navlistPanel(id = "tabsPanel",
tabPanel("one",moduleUI("first")),
tabPanel("two",moduleUI("second"))
))
server <- function(input, output, session){
callModule(module,"first")
callModule(module,"second")
}
shinyApp(ui = ui, server = server)
observeEvent 有效,但由于模块只能看到和知道作为输入参数提供给它们的变量,它不知道指定的 tabsetPanel,因此无法更新它。这个问题可以使用反应值来解决,它作为参数传递并在模块内部更改。一旦更改,主应用程序就会知道它并可以更新 tabsetPanel:
library(shiny)
library(shinydashboard)
moduleUI <- function(id){
ns <- NS(id)
sidebarPanel(
actionButton(ns("action1"), label = "click")
)
}
module <- function(input, output, session, tabsPanel, openTab){
observeEvent(input$action1, {
if(tabsPanel() == "one"){ # input$tabsPanel == "one"
openTab("two")
}else{ # input$tabsPanel == "two"
openTab("one")
}
})
return(openTab)
}
ui <- fluidPage(
h2("Currently open Tab:"),
verbatimTextOutput("opentab"),
navlistPanel(id = "tabsPanel",
tabPanel("one", moduleUI("first")),
tabPanel("two", moduleUI("second"))
))
server <- function(input, output, session){
openTab <- reactiveVal()
observe({ openTab(input$tabsPanel) }) # always write the currently open tab into openTab()
# print the currently open tab
output$opentab <- renderPrint({
openTab()
})
openTab <- callModule(module,"first", reactive({ input$tabsPanel }), openTab)
openTab <- callModule(module,"second", reactive({ input$tabsPanel }), openTab)
observeEvent(openTab(), {
updateTabItems(session, "tabsPanel", openTab())
})
}
shinyApp(ui = ui, server = server)