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

时间:2019-09-11 13:41:42

标签: r shiny shinymodules

我想创建一个navbarPage,其中每个tabPanel或每个tagList的{​​{1}}在另一个模块中创建。

在“正常”闪亮的应用程序中,我可以使用tabPanel来了解当前选择了哪个标签。 (其中input$navbarPage_IDnavbarPage_ID的分配ID变量

使用模块,我无法获得正确的ID,因为它不会更改。
如何获取服务器模块中所选标签的正确ID?


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

tabPanels


示例2 ,其中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)在UI中创建,而模块中仅tabPanels

tagList

1 个答案:

答案 0 :(得分:2)

您在服务器中两次致电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)