Skip to contents

Opens or closes every node of a shinyTree in the browser, for example from "Expand all" and "Collapse all" buttons. The tree's contents are not resent, so selection, checkboxes and search are kept, and input$tree reports each node's new open state as it would after a user click.

Usage

expandTree(session, treeId)

collapseTree(session, treeId)

Arguments

session

The current session variable.

treeId

The identifier for the shinyTree object.

Value

Called for its side effect; returns NULL invisibly.

Details

To control which nodes start open instead, set stopened = TRUE on them when rendering the tree.

expandTree cannot be used on a tree rendered with renderTree(lazy = TRUE), since it would have to fetch the whole tree, and stops with an error. collapseTree works on any tree.

See also

Examples

if (interactive()) {
  library(shiny)
  ui <- fluidPage(
    actionButton("expand", "Expand all"),
    actionButton("collapse", "Collapse all"),
    shinyTree("tree")
  )
  server <- function(input, output, session) {
    output$tree <- renderTree(list(a = list(b = list(c = ""))))
    observeEvent(input$expand, expandTree(session, "tree"))
    observeEvent(input$collapse, collapseTree(session, "tree"))
  }
  shinyApp(ui, server)
}