如何将顶部导航(navbarPage)和侧边栏菜单(sidebarMenu)组合在一起

jmj*_*mjr 8 r sidebar shiny shinydashboard

我有一个闪亮的应用程序(使用navbarPage)有很多选项卡,并希望添加一个侧栏菜单,无论选择哪个选项卡都可以看到.侧栏中的输入值会影响所有选项卡的内容.此外,应该可以隐藏sidebarMenu,因为它在shinydashboard中.

我看到两种可能的方式:

(A)使用shinydashboard并以某种方式添加顶部导航栏或

(B)使用navbarPage并以某种方式添加可隐藏的侧边栏菜单.

(A)使用shinydashboard,最接近我想要的是(简化的MWE):

library("shiny")
library("shinydashboard")

cases <- list(A=seq(50,500, length.out=10), B=seq(1000,10000, length.out=10))

ui <- dashboardPage(
  dashboardHeader(title = "dash w/ navbarMenu"),
  dashboardSidebar(selectizeInput('case', 'Pick a case', selected="A", choices = c("A", "B"), multiple = FALSE), numericInput('num', 'Number', min = 1, max = 10, value = 1, step = 1)),
  dashboardBody(
    tabsetPanel(
      tabPanel(h4("Perspective 1"),
               tabsetPanel(
                 tabPanel("Subtab 1.1", plotOutput("plot11")),
                 tabPanel("Subtab 1.2")
               )),
      tabPanel(h4("Perspective 2"),
               tabsetPanel(
                 tabPanel("Subtab 2.1"),
                 tabPanel("Subtab 2.2")
               ))
    )
  )
)

server <- function(input, output) {
  output$plot11 <- renderPlot({
    hist(rnorm(cases[[input$case]][input$num]))
  })
}

shinyApp(ui, server)
Run Code Online (Sandbox Code Playgroud)

这很难看,因为导航栏菜单是不属于菜单的标签集.我想要的是:dashboard_w_navbar

基于这篇文章,我想在顶部菜单中根本不可能包含"Perspective 1"和"Perspective 2"选项卡,因此使用shinydashboard似乎不可行.

(B)使用navbarPage,我尝试使用navlistPanel(),但我没有成功

(1)使其行为像sidebarMenu,即在页面左侧整体可见

(2)添加隐藏功能.这是我的尝试:

library("shiny")

cases <- list(A=seq(50,500, length.out=10),
              B=seq(1000,10000, length.out=10))

ui <- navbarPage(title = "nav w/ sidebarMenu",
                   tabPanel(h4("Perspective 1"),
                            tabsetPanel(
                              tabPanel("Subtab 1.1",
                                       plotOutput("plot11")),
                              tabPanel("Subtab 1.2")
                            )),
                   tabPanel(h4("Perspective 2"),
                            tabsetPanel(
                              tabPanel("Subtab 2.1"),
                              tabPanel("Subtab 2.2")
                            )),

                 navlistPanel(widths = c(2, 2), "SidebarMenu",
                              tabPanel(selectizeInput('case', 'Pick a case', selected="A", choices = c("A", "B"), multiple = FALSE)),
                              tabPanel(numericInput('num', 'Number', min = 1, max = 10, value = 1, step = 1))
                 )
)


server <- function(input, output) {
  output$plot11 <- renderPlot({
    hist(rnorm(cases[[input$case]][input$num]))
  })
}

shinyApp(ui, server)
Run Code Online (Sandbox Code Playgroud)

再一次,我想要的是: nav_w_sidebar

我知道,有flexDashboard.由于三个原因,它无法解决问题:

(1)我认为无法隐藏侧边栏菜单,因为它是一列而不是真正的侧边栏菜单,

(2)我的应用程序中不需要它,

(3)我认为dataTables不起作用,我也需要它.

此外,我宁愿不必将代码更改为Rmarkdown语法.

我最好使用navbarPage并添加sidebarMenu,因为我的应用程序已经使用navbarPage构建.

SBi*_*sta 5

您可以使用sidebarLayout并执行以下操作:

ui <- fluidPage(sidebarLayout(
  sidebarPanel(navlistPanel(
    widths = c(12, 12), "SidebarMenu",
    tabPanel(selectizeInput('case', 'Pick a case', selected="A", choices = c("A", "B"), multiple = FALSE)),
    tabPanel(numericInput('num', 'Number', min = 1, max = 10, value = 1, step = 1))
  )),
      mainPanel(navbarPage(title = "nav w/ sidebarMenu",

                            tabPanel(h4("Perspective 1"),
                                     tabsetPanel(
                                       tabPanel("Subtab 1.1",
                                                plotOutput("plot11")),
                                       tabPanel("Subtab 1.2")
                                     )),
                            tabPanel(h4("Perspective 2"),
                                     tabsetPanel(
                                       tabPanel("Subtab 2.1"),
                                       tabPanel("Subtab 2.2")
                                     )))

      )
    ))
Run Code Online (Sandbox Code Playgroud)

您得到的是这样的: 在此处输入图片说明

另一个选择是使用fluidRow功能。像这样:

  ui <- fluidPage(
    fluidRow(
      column(3, navlistPanel(
        widths = c(12, 12), "SidebarMenu",
        tabPanel(selectizeInput('case', 'Pick a case', selected="A", choices = c("A", "B"), multiple = FALSE)),
        tabPanel(numericInput('num', 'Number', min = 1, max = 10, value = 1, step = 1))
      )),
      column(9,  navbarPage(title = "nav w/ sidebarMenu",

                             tabPanel(h4("Perspective 1"),
                                      tabsetPanel(
                                        tabPanel("Subtab 1.1",
                                                 plotOutput("plot11")),
                                        tabPanel("Subtab 1.2")
                                      )),
                             tabPanel(h4("Perspective 2"),
                                      tabsetPanel(
                                        tabPanel("Subtab 2.1"),
                                        tabPanel("Subtab 2.2")
                                      ))))


    )
      )
Run Code Online (Sandbox Code Playgroud)

为了得到这个: 在此处输入图片说明

希望能帮助到你!