Skip to contents

shinyHierarchy adds a hierarchical, slicer-style input to Shiny apps. You give it a data frame and the columns that form the hierarchy, from top to bottom. Users then select whole branches or single leaves with tri-state checkboxes.

A minimal app

library(shiny)
library(shinyHierarchy)

df <- data.frame(
  year  = c(2026, 2026, 2026, 2025),
  month = c("January", "January", "February", "December"),
  day   = c(1, 2, 1, 31)
)

ui <- fluidPage(
  hierarchyInput(
    inputId = "dates",
    label   = "Date",
    data    = df,
    levels  = c("year", "month", "day")
  ),
  verbatimTextOutput("value")
)

server <- function(input, output, session) {
  output$value <- renderPrint(hierarchyPath(input$dates, "month"))
}

shinyApp(ui, server)

Any hierarchy works, for example c("country", "department", "city") or c("category", "subcategory", "product").

Node ids

Each node has an id built from its path, joined with ::. In the data above, "2026" is the year, "2026::January" the month, and "2026::January::1" the day. Use these ids in selected and expanded.

tree <- buildHierarchyTree(df, c("year", "month", "day"))
tree[[1]]$id

Appearance

Two arguments control how the input looks when the page loads.

display decides whether the tree is always visible:

  • "inline" (default) keeps the tree on the page, like a dashboard slicer.
  • "dropdown" shows a compact field with a summary of the selection. The tree opens on click and closes when the user clicks outside, like selectInput().

expand decides which levels start unfolded:

  • "first" (default) unfolds the top level only.
  • "all" unfolds every level.
  • "none" starts fully collapsed.

expanded unfolds specific nodes instead.

hierarchyInput(
  "dates", "Date", df, c("year", "month", "day"),
  display  = "dropdown",
  expand   = "none",
  selected = "2026::January",
  search   = TRUE,
  width    = "350px",
  height   = "300px"
)

Reading the value

input$<id> is a structured list (schema_version = 1). Use the helpers rather than reading it directly:

Helper Returns
hierarchyResolved() Every selected leaf, with its full path.
hierarchyRollup() The smallest set of nodes that describes the selection (a fully selected month instead of all its days).
hierarchyPath() The selected values at one level, for example all selected months.
server <- function(input, output, session) {
  observe({
    months <- hierarchyPath(input$dates, "month")
    leaves <- hierarchyResolved(input$dates)
    message(length(leaves), " days selected in ", length(months), " months")
  })
}

Updating from the server

updateHierarchyInput() changes the input from the server. Sending new data, selected or clear = TRUE also updates input$<id>, so reactive code sees the new selection right away. Sending only expanded or open changes the display and leaves the value as it is.

server <- function(input, output, session) {
  # Select the current month when the session starts.
  # The id must match the values in your data (here: English month names).
  current <- format(Sys.Date(), "%Y::%B")
  updateHierarchyInput(session, "dates", selected = current)

  observeEvent(input$reset, {
    updateHierarchyInput(session, "dates", clear = TRUE)
  })

  observeEvent(input$reload, {
    updateHierarchyInput(
      session, "dates",
      data     = new_df,
      levels   = c("year", "month", "day"),
      selected = current
    )
  })

  observeEvent(input$show, {
    updateHierarchyInput(session, "dates", open = TRUE, expanded = "2026")
  })
}

Security

The browser controls the values in input$<id>. Before using selected ids to query or filter sensitive data, check them against the server-side hierarchy and the user’s permissions.

Examples

The package ships runnable apps:

shiny::runApp(system.file("examples", package = "shinyHierarchy"))
shiny::runApp(shiny::shinyAppFile(
  system.file("examples", "update-app.R", package = "shinyHierarchy")
))