tree-server: Manipulate a tree server-side

tree-serverR Documentation

Manipulate a tree server-side

Description

Interact with a tree contructed with treeviewInput() or treecheckInput() from the server.

Usage

searchTreeview(
  inputId,
  pattern,
  ignore_case = TRUE,
  exact_match = FALSE,
  reveal_results = TRUE,
  collapse_before = TRUE,
  session = shiny::getDefaultReactiveDomain()
)

clearSearchTreeview(inputId, session = shiny::getDefaultReactiveDomain())

expandTreeview(
  inputId,
  nodeId = NULL,
  levels = 1,
  session = shiny::getDefaultReactiveDomain()
)

collapseTreeview(
  inputId,
  nodeId = NULL,
  session = shiny::getDefaultReactiveDomain()
)

updateTreeview(
  inputId,
  label = NULL,
  selected = NULL,
  session = shiny::getDefaultReactiveDomain()
)

Arguments

inputId

The id of the input object.

pattern

Pattern to search for.

ignore_case

Case insensitive.

exact_match

Like or equals.

reveal_results

Reveal matching nodes.

collapse_before

Collapse all nodes before revealing results.

session

The session object passed to function given to shinyServer.

nodeId

Id of the node to expand or collapse, use ⁠input$<inputId>_nodes⁠ to see the Ids. If NULL expand the all tree.

levels

Levels to expand.

label

Display label for the control, or NULL for no label.

selected

Default selected value, must correspond to the Id of the node.

Value

No value, side-effects only.

Examples

# searchTreeview ----------------------------------------------------------


library(shiny)
library(shinytreeview)

data("countries")

ui <- fluidPage(
  tags$h3("treeviewInput search example"),
  fluidRow(
    column(
      width = 4,
      treeviewInput(
        inputId = "country",
        label = "Choose a country:",
        choices = make_tree(
          countries, c("continent", "subregion", "name")
        ),
        width = "100%"
      )
    ),
    column(
      width = 8,
      textInput("search", "Search a country"),
      tags$b("Selected country:"),
      verbatimTextOutput(outputId = "result")
    )
  )
)

server <- function(input, output, session) {
  output$result <- renderPrint({
    input$country
  })

  observeEvent(input$search, {
    if (isTruthy(input$search)) {
      searchTreeview(
        inputId = "country",
        pattern = input$search,
        reveal_results = TRUE
      )
    } else {
      clearSearchTreeview("country")
      collapseTreeview("country")
    }
  })
}

if (interactive())
  shinyApp(ui, server)



# expand/collapse ---------------------------------------------------------


library(shiny)
library(shinytreeview)

data("countries")

ui <- fluidPage(
  tags$h3("treeviewInput expand/collapse example"),
  fluidRow(
    column(
      width = 4,
      treeviewInput(
        inputId = "country",
        label = "Choose a country:",
        choices = make_tree(
          countries, c("continent", "subregion", "name")
        ),
        nodes_input = TRUE,
        width = "100%"
      )
    ),
    column(
      width = 8,
      actionButton("expandAll", "Expand all"),
      actionButton("expandWAfrica", "Expand Western Africa"),
      actionButton("collapseAll", "Collapse all"),
      tags$br(),
      tags$b("Selected country:"),
      verbatimTextOutput(outputId = "result")
    )
  )
)

server <- function(input, output, session) {

  output$result <- renderPrint({
    input$country
  })

  observeEvent(input$expandAll, {
    expandTreeview("country", levels = 3)
  })

  observeEvent(input$expandWAfrica, {
    nodes <- input$country_nodes
    w_africa <- nodes[nodes$text == "Western Africa", "nodeId"]
    print(w_africa)
    expandTreeview("country", nodeId = w_africa)
  })

  observeEvent(input$collapseAll, {
    collapseTreeview("country")
  })
}

if (interactive())
  shinyApp(ui, server)



# updateTreeview ----------------------------------------------------------


library(shiny)
library(shinytreeview)

data("cities")

ui <- fluidPage(
  tags$h3("Update label & selected value"),
  treeviewInput(
    inputId = "tree",
    label = "Choose a city:",
    choices = make_tree(cities, c("continent", "country", "city")),
    multiple = FALSE,
    prevent_unselect = TRUE
  ),
  verbatimTextOutput(outputId = "result"),
  textInput("label", "New label:", "Choose a city:"),
  radioButtons(
    "selected", "Selected:",
    choices = unique(c(cities$continent, cities$country, cities$city)),
    inline = TRUE
  )
)

server <- function(input, output, session) {
  output$result <- renderPrint({
    input$tree
  })
  observe(updateTreeview(inputId = "tree", label = input$label))
  observe(updateTreeview(inputId = "tree", selected = input$selected))
}

if (interactive())
  shinyApp(ui, server)

dreamRs/shinytreeview documentation built on July 3, 2024, 7:02 a.m.