如何在带有模块的 navbarPage 中获取选定的 Tab-ID

How to get the selected Tab-ID in a navbarPage with modules

我想创建一个 navbarPage,其中每个 tabPanel 或每个 tabPaneltagList 是在另一个模块中创建的。

在 "normal" 闪亮的应用程序中,我可以通过使用 input$navbarPage_ID 知道当前选择了哪个选项卡。 (其中navbarPage_IDnavbarPage的赋值id变量)

对于模块,我无法获得正确的 ID,因为它不会改变。
如何获取服务器模块中所选选项卡的正确 ID?


示例 1,其中 tabPanels 在模块中创建:

library(shiny)
## Module 1 ####################
mod1_ui <- function(id, label, navid) {
  ns <- NS(id)
  tabPanel(label, value = navid,
           h2("mod1")
  )
}
mod1_server <- function(input, output, session, navid) {
  observe({
    message("mod1_server ", navid)
  })
}
## Module 2 ####################
mod2_ui <- function(id, label, navid) {
  ns <- NS(id)
  tabPanel(label, value = navid,
           h2("mod2")
  )
}
mod2_server <- function(input, output, session, navid) {
  observe({
    message("mod2_server ", navid)
  })
}

## Shiny App #####################
ui <- navbarPage(collapsible  = T, id = "navbarid",
                 title = "Title",
                 mod1_ui("mod1", "Module 1 Tab", navid = 1),
                 mod2_ui("mod2", 'Module 2 Tab', navid = 2)
)

server <- function(input, output, session) {
  callModule(mod1_server, "mod1", input$navbarid)
  callModule(mod2_server, "mod2", input$navbarid)
}

shinyApp(ui, server)


示例 2,其中 tabPanels 在 UI 中创建,并且在模块中只有一个 tagList

library(shiny)
## Module 1 ####################
mod1_ui <- function(id, label, navid) {
  ns <- NS(id)
  tagList(h2("mod1"))
}
mod1_server <- function(input, output, session, navid) {
  observe({ 
    message("mod1_server ", navid)
    # message("mod1_server ", input$navbarid)
  })
}
## Module 2 ####################
mod2_ui <- function(id, label, navid) {
  ns <- NS(id)
  tagList(h2("mod2"))
}
mod2_server <- function(input, output, session, navid) {
  observe({
    message("mod2_server ", navid)
    # message("mod2_server ", input$navbarid)
  })
}

## Shiny App #####################
ui <- navbarPage(collapsible  = T, id = "navbarid",
                 title = "Title",
                 
                 tabPanel("Module 1 Tab", value = 1,
                          mod1_ui("mod1")
                 ),
                 tabPanel("Module 2 Tab", value = 2,
                          mod2_ui("mod2")
                 )
)

server <- function(input, output, session) {
  callModule(mod1_server, "mod1", input$navbarid)
  callModule(mod2_server, "mod2", input$navbarid)
}

shinyApp(ui, server)

您在服务器中调用了两次 mod1_server ;p

我将在父会话中使用该技巧:

library(shiny)
## Module 1 ####################
mod1_ui <- function(id, label, navid) {
  ns <- NS(id)
  tabPanel(label, value = navid,
           h2("mod1")
  )
}
mod1_server <- function(input, output, session, parent_session) {
  observe({
    message("mod1_server ", parent_session$input$navbarid)
  })
}
## Module 2 ####################
mod2_ui <- function(id, label, navid) {
  ns <- NS(id)
  tabPanel(label, value = navid,
           h2("mod2")
  )
}
mod2_server <- function(input, output, session, parent_session) {
  observe({
    message("mod2_server ", parent_session$input$navbarid)
  })
}

## Shiny App #####################
ui <- navbarPage(collapsible  = T, id = "navbarid",
                 title = "Title",
                 mod1_ui("mod1", "Module 1 Tab", navid = 1),
                 mod2_ui("mod2", 'Module 2 Tab', navid = 2)
)

server <- function(input, output, session) {
  callModule(mod1_server, "mod1", parent_session = session)
  callModule(mod2_server, "mod2", parent_session = session)
}

shinyApp(ui, server)