0

我有一个很好的动态生成的 shinyTree,它在闪亮仪表板的主体中显示得很漂亮。这太棒了。但是,我真正想要的是它位于侧边栏中,因此可以用来选择一个选项卡。但无论我尝试什么,我似乎都无法让它出现在侧边栏中。

下面不工作的代码:

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 = "")
      )
    )
  })
})

我觉得我错过了一些非常明显的东西,但我在谷歌和这里的搜索并没有带来任何东西。

4

1 回答 1

0

所以我是个白痴。显然你只能有一个闪亮树的实例,所以身体中的 shihyTree 阻止了侧边栏中的闪亮树出现。

下面的工作代码:

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)
于 2021-05-22T11:13:11.950 回答