Search code examples
rshinytabitem

How to trigger certain code depending on the user's tabItem choice in R-Shiny?


I would like to execute a block of commands when the user selects a specific "tabItem" from my "sidebarmenu". How can I trigger this chain of events?

sidebarMenu(
  menuItem("File", tabName = "tabFile"),
  menuItem("Data", tabName = "tabData")
)

...

observe({
  if(input$tabFile==TRUE) { a-block-of-commands }
})

Solution

  • This is explained in ?sidebarMenu.

    sidebarMenu(
      id = "tabs", 
      menuItem("File", tabName = "tabFile"),
      menuItem("Data", tabName = "tabData")
    )
    
    ...
    
    observe({
      if(input$tabs == "tabFile") { a-block-of-commands }
    })