【发布时间】:2018-02-19 02:23:29
【问题描述】:
我有一个带有许多选项卡的闪亮应用程序(使用 navbarPage),我想添加一个无论选择哪个选项卡都可以看到的侧边栏菜单。侧边栏中的输入值会影响所有选项卡的内容。 此外,应该可以隐藏 sidebarMenu,因为它在闪亮的仪表板中。
我看到了两种可能的方式:
(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)
基于这个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) 它不是我在应用程序中需要的响应式,
(3) 我认为dataTables 不工作,我也需要。
此外,我宁愿不必将代码更改为 Rmarkdown 语法。
我最好使用 navbarPage 并添加一个 sidebarMenu,因为我的应用程序已经使用 navbarPage 构建了。
【问题讨论】:
标签: r shiny sidebar shinydashboard