好的,这是针对您的情况使用jsTreeR
. 该代码可以正常工作并执行我认为您正在寻找的内容,但它不如shinyWidgets
. 我想有一种方法可以结合这种方法(主要取自文档中的 jsTreeR 示例)和本文中创建绑定的方法,以创建看起来不错并具有您正在寻找的功能的东西。
library(shiny)
library(jsTreeR)
library(jsonlite)
#create nodes
nodes <- list(
list(
text="List A",
type="root",
children = list(
list(
text = "Option 1",
type = "child"
),
list(
text = "Option 2",
type = "child"
),
list(
text = "Option 3",
type = "child"
),
list(
text = "Option 4",
type = "child"
),
list(
text = "Option 5",
type = "child"
)
)
),
list(
text="List B",
type="root",
children = list(
list(
text = "Option 6",
type = "child"
),
list(
text = "Option 7",
type = "child"
),
list(
text = "Option 8",
type = "child"
),
list(
text = "Option 9",
type = "child"
),
list(
text = "Option 10",
type = "child"
)
)
)
)
types <- list(
root = list(
icon = "none"
),
child = list(
icon = "none"
)
)
#Use in shiny context - example taken from documentation for jsTreeR
ui <- fluidPage(
br(),
fluidRow(
column(width = 4,
jstreeOutput("jstree")
),
column(width = 4,
tags$fieldset(
tags$legend("Selections - JSON format"),
verbatimTextOutput("treeSelected_json")
)
),
column(width = 4,
tags$fieldset(
tags$legend("Selections - R list"),
verbatimTextOutput("treeSelected_R")
)
)
)
)
server <- function(input, output) {
output[["jstree"]] <- renderJstree(
jstree(nodes, checkboxes = TRUE, multiple=TRUE, types=types)
)
output[["treeSelected_json"]] <- renderPrint({
toJSON(input[["jstree_selected"]], pretty = TRUE, auto_unbox = TRUE)
})
output[["treeSelected_R"]] <- renderPrint({
input[["jstree_selected"]]
})
}
shinyApp(ui, server)
请注意,节点上没有附加数据——这只是获得了正确的 UI 功能。您必须将值附加到可以用于下游计算的节点。