Search code examples
shinyjstreeshinytree

shinyTree: more than 3 hierarchical levels


I have the following code:

library(shiny)
library(shinyTree)

server <- shinyServer(function(input, output, session) {  
  output$level_tree <- renderTree({ 
    list('1 level'= list( 
      '1.1 level' = structure(
        list('1.1.1 level'='1', 
            '1.1.2 level'='2')),stopened=TRUE),  
      '1.2 level' = structure(
        list('1.2.1 level'='3')),stopened=TRUE))) 
  })
})

ui <- shinyUI(
  fluidPage(
    h4('Levels'),
    shinyTree("level_tree", checkbox = TRUE)
  )
)
shinyApp(ui, server)

Is there any way to the add to the hierarchical tree the following levels:

  • 1.1.1.1,

  • 1.1.1.1.1,

  • 1.1.1.1.1.1

I unsuccessfully tried:

list('1 level'= list( 
      '1.1 level' = structure(
        list('1.1.1 level'=list(
                '1.1.1.1 level=list(
                    '1.1.1.1.1'='1')), 
            '1.1.2 level'='2')),stopened=TRUE),  
      '1.2 level' = structure(
        list('1.2.1 level'='3')),stopened=TRUE))) 

Solution

  • Yes, you have to nest everyhing in lists or structures.

    Your data was in a wrong format. This data should work.

    list(
      '1 level'= list( 
        '1.1 level' = structure(
          list('1.1.1 level'=
                 list('1.1.1.1 level'=
                        list('1.1.1.1.1'='1')),
               '1.1.2 level'='2'),stopened=TRUE)
      ),
      '1.2 level' = structure(
        list('1.2.1 level' = '3'),stopened=TRUE)
    )
    

    Shiny-App

    library(shiny)
    library(shinyTree)
    library(shinyjs)
    
    trl <- list(
      '1 level'= list( 
        '1.1 level' = structure(
          list('1.1.1 level'=
                 list('1.1.1.1 level'=
                        list('1.1.1.1.1'='1')),
               '1.1.2 level'='2'),stopened=TRUE)
      ),
      '1.2 level' = structure(
        list('1.2.1 level' = '3'),stopened=TRUE)
    )
    
    ui <- fluidPage(
      shinyTree("tree")
    )
    
    server <- function(input, output, session) {
      output$tree <- renderTree({
        trl
      })
    }
    
    shinyApp(ui,server)