SeGa
SeGa

Reputation: 9809

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

I want to create a navbarPage, where each tabPanel or the tagList of each tabPanel is created in another module.

In a "normal" shiny app I would be able to know which Tab is currently selected by using input$navbarPage_ID. (where navbarPage_ID is the assigned id variable of navbarPage)

With modules I am not able to get the correct ID, as it doesnt change.
How do I get the correct ID of the selected Tab in the server module?


Example 1, where tabPanels are created in modules:

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)


Example 2, where tabPanels are created in the UI and only a tagList in modules:

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)

Upvotes: 4

Views: 1601

Answers (1)

Victorp
Victorp

Reputation: 13856

You call mod1_server twice in your server ;p

I'll use the trick with the parent session :

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)

Upvotes: 5

Related Questions