Search code examples
rshinyshinydashboardshinytreeshinydashboardplus

ShinyTree in ShinyDashboard sidebar


I have a nice dynamically produced shinyTree that displays beautifully in the main body of a shinydashboard. Which is wonderful. However, what I actually want is for it to be in the sidebar so it can be used to select a tab. But no matter what I try, I cant seem to get it to appear in the sidebar.

Not working code below:

library(shiny)
library(shinyTree)
library(shinydashboard)
library(shinydashboardPlus)

header <- dashboardHeader(title = "MWE")
body <- dashboardBody(
  # works fine in the body
  shinyTree("tree", stripes = TRUE, multiple = FALSE, animation = FALSE)
)
sidebar <- dashboardSidebar(
  # doesn't work here
  # shinyTree("tree", stripes = TRUE, multiple = FALSE, animation = FALSE)
  sidebarMenu(
    # doesn't work here
    # shinyTree("tree", stripes = TRUE, multiple = FALSE, animation = FALSE)
    menuItem("test"
         # or here
         # shinyTree("tree", stripes = TRUE, multiple = FALSE, animation = FALSE)
    )
  )
)

shinyUI(
  dashboardPage(
   header = header,
   sidebar = sidebar,
   body = body
  )
)

shinyServer(function(input, output, session) {
  # Simplified test tree
  output$tree <- renderTree({
    list(
      root1 = "",
      root2 = list(
        SubListA = list(leaf1 = "", leaf2 = "", leaf3=""),
        SubListB = list(leafA = "", leafB = "")
      ),
      root3 = list(
        SubListA = list(leaf1 = "", leaf2 = "", leaf3=""),
        SubListB = list(leafA = "", leafB = "")
      )
    )
  })
})

I feel like I'm missing something really obvious, but my searching on google and here hasn't brought anything up.


Solution

  • So I'm an idiot. Apparently you can only have a single instance of a shinyTree, so the shihyTree in the body was preventing the shinyTree in the sidebar show up.

    Working code below:

    library(shiny)
    library(shinyTree)
    library(shinydashboard)
    library(shinydashboardPlus)
    
    header <- dashboardHeader(title = "MWE")
    body <- dashboardBody(
        # Don't put the shinyTree in the body!
        # shinyTree("tree", stripes = TRUE, multiple = FALSE, animation = FALSE)
    )
    sidebar <- dashboardSidebar(
        #shinyTree("tree", stripes = TRUE, multiple = FALSE, animation = FALSE),
        sidebarMenu(
            shinyTree("tree", stripes = TRUE, multiple = FALSE, animation = FALSE)
        )
    )
    
    
    ui <-   dashboardPage(
            header = header,
            sidebar = sidebar,
            body = body
        )
    
    
    server <- function(input, output, session) {
        # Simplified test tree
        output$tree <- renderTree({
            list(
                root1 = "",
                root2 = list(
                    SubListA = list(leaf1 = "", leaf2 = "", leaf3=""),
                    SubListB = list(leafA = "", leafB = "")
                ),
                root3 = list(
                    SubListA = list(leaf1 = "", leaf2 = "", leaf3=""),
                    SubListB = list(leafA = "", leafB = "")
                )
            )
        })
    }
    
    shinyApp(ui, server)