我有一个带有许多选项卡的 Shiny 应用程序(使用 navbarPage),并且想添加一个无论选择哪个选项卡都可以看到的侧边栏菜单。侧栏中的输入值会对所有选项卡的内容产生影响。 此外,应该可以隐藏侧边栏菜单,因为它位于 Shiny 的仪表板中。
我看到两种可能的方法:
(A) 使用 Shiny 的仪表板并以某种方式添加顶部导航栏或
(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)
这很丑陋,因为导航栏菜单是选项卡集,不属于菜单的一部分。我想要的是:
基于此post ,我想根本不可能在顶部菜单中包含“Perspective 1”和“Perspective 2”选项卡,因此使用shinydashboard似乎不可行。
(B) 使用 navbarPage,我尝试使用 navlistPanel() 但没有成功
(1) 使其表现得像侧边栏菜单,即在页面左侧整体可见,并且
(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)
我知道,有flexDashboard 。它没有解决问题有以下三个原因:
(1) 我认为不可能隐藏侧边栏菜单,因为它是一列而不是真正的侧边栏菜单,
(2) 它不是我在应用程序中需要的响应式(Reactive),
(3) 我认为 dataTables 不起作用,但我也需要它。
此外,我不想将代码更改为 Rmarkdown 语法。
我最好使用 navbarPage 并添加侧边栏菜单,因为我的应用程序已经使用 navbarPage 构建。
最佳答案
您可以使用 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")
)))
)
))
另一种选择是使用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")
))))
)
)
希望对你有帮助!
关于r - 如何以 Shiny 的方式组合顶部导航(navbarPage)和侧边栏菜单(sidebarMenu),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46145608/