Learn R Programming

bslib (version 0.8.0)

nav_select: Dynamically update nav containers

Description

Functions for dynamically updating nav containers (e.g., select, insert, and remove nav items). These functions require an id on the nav container to be specified and must be called within an active Shiny session.

Usage

nav_select(id, selected = NULL, session = get_current_session())

nav_insert( id, nav, target = NULL, position = c("after", "before"), select = FALSE, session = get_current_session() )

nav_remove(id, target, session = get_current_session())

nav_show(id, target, select = FALSE, session = get_current_session())

nav_hide(id, target, session = get_current_session())

Arguments

id

a character string used to identify the nav container.

selected

a character string used to identify a particular nav_panel() item.

session

a shiny session object (the default should almost always be used).

nav

a nav_panel() item.

target

The value of an existing nav_panel() item, next to which tab will be added. If removing: the value of the nav_panel() item that you want to remove.

position

Should nav be added before or after the target?

select

Should nav be selected upon being inserted?

See Also

Navset functions create the navigation container holding the nav panels.

nav_panel(), nav_panel_hidden() create panels of content.

nav_menu(), nav_item(), nav_spacer() create menus, items, or space in the navset control area.

Other Panel container functions: nav-items, navset

Examples

Run this code

can_browse <- function() rlang::is_interactive() && require("shiny")

# Selecting a tab
if (can_browse()) {
  shinyApp(
    page_fluid(
      radioButtons("item", "Choose", c("A", "B")),
      navset_hidden(
        id = "container",
        nav_panel_hidden("A", "a"),
        nav_panel_hidden("B", "b")
      )
    ),
    function(input, output) {
      observe(nav_select("container", input$item))
    }
  )
}

# Inserting and removing
if (can_browse()) {
  ui <- page_fluid(
    actionButton("add", "Add 'Dynamic' tab"),
    actionButton("remove", "Remove 'Foo' tab"),
    navset_tab(
      id = "tabs",
      nav_panel("Hello", "hello"),
      nav_panel("Foo", "foo"),
      nav_panel("Bar", "bar tab")
    )
  )
  server <- function(input, output) {
    observeEvent(input$add, {
      nav_insert(
        "tabs", target = "Bar", select = TRUE,
        nav_panel("Dynamic", "Dynamically added content")
      )
    })
    observeEvent(input$remove, {
      nav_remove("tabs", target = "Foo")
    })
  }
  shinyApp(ui, server)
}

Run the code above in your browser using DataLab