tree-server | R Documentation |
Interact with a tree contructed with treeviewInput()
or treecheckInput()
from the server.
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()
)
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 |
levels |
Levels to expand. |
label |
Display label for the control, or |
selected |
Default selected value, must correspond to the Id of the node. |
No value, side-effects only.
# 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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.