make_tree: Create choice structure for 'treeviewInput()'

View source: R/input-treeview.R

make_treeR Documentation

Create choice structure for treeviewInput()

Description

Create choice structure for treeviewInput()

Usage

make_tree(data, levels, selected = NULL, levels_id = NULL, ...)

Arguments

data

A data.frame.

levels

Variables identifying hierarchical levels, values of those variables will be used as text displayed.

selected

Default selected value(s).

levels_id

Variable to use as ID, thos values wont't be displayed but can be retrieved server-side with return_value = "id".

...

Named arguments with list of attributes to apply to a certain level. Names must be the same as the levels. See full list of attributes.

Value

a list that can be used in treeviewInput() or treecheckInput().

Examples

library(shinytreeview)

data("cities")
head(cities)

# Create choices that can be used in treeviewInput
make_tree(cities, c("continent", "country", "city"))

# Custom attributes for continent level
make_tree(cities, c("continent", "country", "city"), continent = list(selectable = FALSE))




library(shiny)
ui <- fluidPage(
  tags$h3("treeviewInput cities example"),
  fluidRow(
    column(
      width = 6,
      treeviewInput(
        inputId = "tree1",
        label = "Choose an area:",
        choices = make_tree(cities, c("continent", "country", "city")),
        multiple = FALSE,
        prevent_unselect = TRUE
      ),
      verbatimTextOutput(outputId = "result1")
    ),
    column(
      width = 6,
      treeviewInput(
        inputId = "tree2",
        label = "Choose an area (continent not selectable):",
        choices = make_tree(
          cities, c("continent", "country", "city"),
          continent = list(selectable = FALSE)
        ),
        multiple = FALSE,
        prevent_unselect = TRUE
      ),
      verbatimTextOutput(outputId = "result2")
    )
  )
)

server <- function(input, output, session) {
  output$result1 <- renderPrint({
    input$tree1
  })
  output$result2 <- renderPrint({
    input$tree2
  })
}

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

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