checkboxTreeInput: Checkbox tree

Description Usage Arguments See Also Examples

View source: R/checkboxTree.R

Description

This creates a checkbox tree in the Shiny UI.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
checkboxTreeInput(
  inputId,
  nodes,
  sort = FALSE,
  single = FALSE,
  checkModel = "leaf",
  checked = list(),
  onlyLeafCheckboxes = FALSE,
  showExpandAll = FALSE
)

Arguments

inputId

the input slot that will be used to access the value

nodes

a list of nodes; each node is a named list with the following fields:

label

node label - required

value

the value associated to the node - required

children

the children of the node, i.e. a list of nodes

className

a class name to add to the node

disabled

logical, whether to disable the node

showCheckbox

logical, whether the node should show a checkbox

title

a custom title attribute for the node

sort

logical, whether to sort the nodes by their label

single

logical; if TRUE, only one node can be selected

checkModel

"leaf" or "all", specifies which checked nodes should be included in the value

checked

a list of initially checked nodes, identified by their value

onlyLeafCheckboxes

logical, whether checkboxes should be shown only for the leaves

showExpandAll

logical; if TRUE, buttons for expanding and collapsing all parent nodes will appear in the widget

See Also

updateCheckboxTreeInput

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
if(interactive()) {

# make the nodes list from a vector of file paths
makeNodes <- function(leaves){
  dfs <- lapply(strsplit(leaves, "/"), function(s){
    item <-
      Reduce(function(a,b) paste0(a,"/",b), s[-1], s[1], accumulate = TRUE)
    data.frame(
      item = item,
      parent = c("root", item[-length(item)]),
      stringsAsFactors = FALSE
    )
  })
  dat <- dfs[[1]]
  for(i in 2:length(dfs)){
    dat <- merge(dat, dfs[[i]], all = TRUE)
  }
  f <- function(parent){
    i <- match(parent, dat$item)
    item <- dat$item[i]
    children <- dat$item[dat$parent==item]
    label <- tail(strsplit(item, "/")[[1]], 1)
    if(length(children)){
      list(
        label = label,
        value = item,
        children = lapply(children, f)
      )
    }else{
      list(label = label, value = item)
    }
  }
  lapply(dat$item[dat$parent == "root"], f)
}

folder <-
  list.files(system.file("www", "shared", package = "shiny"), recursive = TRUE)
nodes <- makeNodes(folder)


library(shiny)
library(shinyCheckboxTree)

ui <- fluidPage(
  tags$head(
    tags$style(HTML(".react-checkbox-tree { font-size: 13px; }"))
  ),
  br(),
  fluidRow(
    column(
      6,
      checkboxTreeInput("tree", nodes = nodes)
    ),
    column(
      6,
      verbatimTextOutput("checked")
    )
  )
)

server <- function(input, output, session) {
  output[["checked"]] <- renderPrint({
    cat(input[["tree"]], sep = "\n")
  })
}

shinyApp(ui, server)

}

stla/shinyCheckboxTree documentation built on Aug. 7, 2020, 2:25 p.m.