Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

scoping issue with updateNavbarPage() function from within shiny module

Tags:

module

r

shiny

I am trying to build an app where the user is able to switch tabs when clicking on a specific object. However, I have developed the app using modules and would like to continue to do so. I am running into a problem with the scoping when trying to call the updateNavbarPage() function from inside of the modules. I have created a MWE example to illustrate the problem.

#==================================================
# MRE for updateNavBar scoping issue within modules
#==================================================

modOneUI <- function(id){
  ns <- NS(id)

  tagList(
    h4(
      "Click this button to change tabs!"
    ),
    actionButton(
      ns("submit"),
      label = "Go to next Tab"
    )
  )
}


modOne <- function(input, output, session){
  observeEvent(input$submit, {
    updateNavbarPage(session, "nav-page", "tab2")
  })
}

ui <- shinyUI(
  navbarPage(
    id = "nav-page",
    title = "Example Navbar Page Issue",
    tabPanel(
      id = "tab1",
      value = "tab1",
      div(
        "Tab 1"
      ),
      div(
        modOneUI("tab1_mod")
      )
    ),
    tabPanel(
      id = "tab2",
      value = "tab2",
      div(
        "Tab 2"
      ), 
      div(
        h4("This is the second tab")
      )
    )
  )
)


server <- shinyServer(function(input, output, session){
  callModule(modOne, "tab1_mod")

})

shinyApp(ui = ui, server = server)

When this app is run, and the action button is clicked on the first tab, nothing happens. However if you remove the module and place the ui and server module code directly into the ui and server portions then clicking the button works. Here is the code with the modules removed.

ui <- shinyUI(
  navbarPage(
    id = "nav-page",
    title = "Example Navbar Page Issue",
    tabPanel(
      id = "tab1",
      value = "tab1",
      div(
        "Tab 1"
      ),
      div(
        h4(
          "Click this button to change tabs!"
        ),
        actionButton(
          "submit",
          label = "Go to next Tab"
        )
      )
    ),
    tabPanel(
      id = "tab2",
      value = "tab2",
      div(
        "Tab 2"
      ), 
      div(
        h4("This is the second tab")
     )
    )
  )
)


server <- shinyServer(function(input, output, session){
  observeEvent(input$submit, {
    updateNavbarPage(session, "nav-page", "tab2")
  })

})

shinyApp(ui = ui, server = server)

Is there any way to use updateNavbarPage() from within a module to switch to a tab that is in not in the module?

like image 210
tbradley Avatar asked Oct 27 '25 04:10

tbradley


1 Answers

Do not ask me why :-) but it works like this:

modOne <- function(input, output, session, x){
  observeEvent(input$submit, {
    updateNavbarPage(x, "nav-page", "tab2")
  })
}
callModule(modOne, "tab1_mod", x=session)
like image 122
Stéphane Laurent Avatar answered Oct 29 '25 20:10

Stéphane Laurent



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!