Description Usage Arguments See Also Examples
This creates a checkbox tree in the Shiny UI.
1 2 3 4 5 6 7 8 9 10 |
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:
|
sort |
logical, whether to sort the nodes by their label |
single |
logical; if |
checkModel |
|
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 |
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)
}
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.