You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

R语言shinyTree如何实现仅最低级子节点显示复选框

实现方案

实现原理是给需要隐藏复选框的非最低层级节点添加自定义样式类,再通过CSS规则隐藏对应节点的复选框即可,修改后的完整可运行代码如下:

library(shiny)
library(shinyTree)

# Create tree data ----
tree.data <-    list(
  'Recreational - Fishing' = structure(list(
    'Boat' = structure(list(
      'Cray pot'= structure("",sttype="default",sticon="glyphicon glyphicon-record"),
      'Hand/rod & line' = structure("",sttype="default",sticon="glyphicon glyphicon-record"),
      'Cray loop' = structure("",sttype="default",sticon="glyphicon glyphicon-record"),
      'Drop net' = structure("",sttype="default",sticon="glyphicon glyphicon-record"),
      'Spear' = structure("",sttype="default",sticon="glyphicon glyphicon-record"),
      'Other' = structure("",sttype="default",sticon="glyphicon glyphicon-record")),
      sttype="default",stopened=FALSE,sticon="glyphicon glyphicon-plus", stdisabled=TRUE, stclass="hide-checkbox")),
    sttype="default",stopened=FALSE,sticon="glyphicon glyphicon-plus", stclass="hide-checkbox")
)

# UI ----
ui <- fluidPage(
  tags$head(
    tags$style(HTML("
      .jstree-node.hide-checkbox > .jstree-checkbox {
        display: none !important;
      }
    "))
  ),
  sidebarLayout(
    sidebarPanel(
      shinyTree("tree", checkbox = TRUE, search=TRUE, searchtime = 1000)
    ),
    mainPanel()
  )
)

#Server ----
server <- function(input, output) {
  # Render Tree
  output$tree <- renderTree({
    tree.data
  })
}

# Run the application 
shinyApp(ui = ui, server = server)

运行后'Recreational - Fishing'和'Boat'两个父节点的复选框会被隐藏,仅最低层级的子节点保留复选框,完全匹配需求。

内容的提问来源于stack exchange,提问作者Brooke Gibbons

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.25 07:36:03