1

Is it possible to have only the lowest level children in a shinyTree have a checkbox? In the below screenshot I would like 'Recreational -Fishing' and 'Boat' to not have a checkbox but all the other children to have a checkbox (in screenshot; circles = checkboz, + = no checkbox)?

Thanks!

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)),
    sttype="default",stopened=FALSE,sticon="glyphicon glyphicon-plus")
  )

# UI ----
ui <- fluidPage(
    sidebarLayout(
        sidebarPanel(
          shinyTree("tree", checkbox = TRUE, search=TRUE, searchtime = 1000)
        ),

        # Show a plot of the generated distribution
        mainPanel(
           
        )
    )
)

#Server ----
server <- function(input, output) {

  # Render Tree
  output$tree <- renderTree({
    tree.data
  })
}

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

screenshot

3 Answers3

3

You can add the following css to your shiny app to hide some checkboxes in shinyTree:

#\31 _anchor > .jstree-checkbox {
    display: none;
}

#\32 _anchor > .jstree-checkbox {
    display: none;
}

enter image description here

danlooo
  • 10,067
  • 2
  • 8
  • 22
  • Thanks for your help. My actual shinyTree has 7 first levels and within those multiple other levels. Is there a way to set all of the top and second levels in css to not have a checkbox? – Brooke Gibbons Nov 25 '21 at 01:01
  • You can right click on a shiny element in your browser and then click on inspect to see the ids of the HTML eleemnts. CSS rules can also contain regular expressions to match multiple ids (e.g. those of the first n levels) – danlooo Nov 25 '21 at 08:14
  • is there are to also make these un-selectable? – Brooke Gibbons Dec 08 '21 at 07:42
  • Sorry that didn't make sense. Is there a way to make the top 1 and 2 levels so you can't select them? do you have any example code of removing the checkbox from all the level 1 and 2s? – Brooke Gibbons Dec 08 '21 at 07:52
  • The example code is written in my answer above. Then, you just need to add these css to your shiny app. – danlooo Dec 08 '21 at 07:55
  • I meant so you can't select the top two levels? (anchors) – Brooke Gibbons Dec 08 '21 at 08:17
  • What does 'can not select' mean? no blue background on click? No plus sign shown? Nothing will change so even the sublevels won't show? – danlooo Dec 08 '21 at 08:20
  • Sorry, I will clarify. At the moment when I click on the top two levels, the anchor clicked is highlighted in blue, and adds all the sub-levels beneath it to the selected data frame (when I run get_selected()). My ideal would be when I click on either of the top two levels then that part of the tree will expand (similar to when clicking on the arrows on the left), not get highlighted in blue and not be added to the get_selected list. – Brooke Gibbons Dec 09 '21 at 00:29
1

add to your css:

.jstree li.jstree-open > a.jstree-anchor > i.jstree-checkbox,
.jstree li.jstree-closed > a.jstree-anchor > i.jstree-checkbox {
   display:none; 
}
Nico_
  • 1,388
  • 17
  • 31
0

In my case I only wanted to disable the checkbox at the top level so my ui looks like:

ui <- fluidPage(
    tags$head(
        tags$style(
            HTML("
            div.jstree > ul.jstree-children > li > a.jstree-anchor > i.jstree-checkbox {
                display:none; 
            }")
        )
    ),
    .
    .
    .
Michael Henry
  • 599
  • 2
  • 4
  • 17