[英]Shiny and shinydashboard: How to display inputs in sidebar only on certain tabs?
I want to display inputs (checkboxes, select input) in the sidebar of my shiny dashboard, but only when a certain tab is clicked.我想在我的 shiny 仪表板的侧栏中显示输入(复选框,select 输入),但仅在单击某个选项卡时。
Minimum reporducible example below.下面的最小可重现示例。 How can I get the checkboxes and select input to only show up when on Page 2?
如何让复选框和 select 输入仅在第 2 页上显示?
#ui.R
library(shiny)
library(shinydashboard)
# Define UI for application that draws a histogram
shinyUI(dashboardPage(
dashboardHeader(title = "Test Application",
titleWidth = "400px"
),
dashboardSidebar(
id = "navbar",
menuItem("Page 1", tabName = "page1"),
menuItem("Page 2", tabName = "page2"),
# THESE SHOW UP ALL THE TIME - HOW TO GET JUST ON PAGE 2?
checkboxGroupInput("outcome", "Select Outcome Variable(s):", choices = c("Box 1", "Box 2", "Box 3")),
selectInput("selectinput", label = "Select:", choices = c("Choice 1", "Choice 2", "Choice 2"))
),
dashboardBody(
tabItems(
tabItem(
tabName = "page1",
h1("This is page 1")
),
tabItem(
tabName = "page2",
h1("This is page 2")
)
)
)
))
I assume something is needed in here to make the inputs dynamic?我认为这里需要一些东西来使输入动态化?
# server.R
library(shiny)
# Define server logic required to draw a histogram
shinyServer(function(input, output) {
})
ANSWER: use a conditional panel that queries the selected tab.答案:使用查询选定选项卡的条件面板。
Credit: Mine at Rstudio信用:我在Rstudio
library(shiny)
library(shinydashboard)
# ui ---------------------------------------------------------------------------
ui <- dashboardPage(
# title ----
dashboardHeader(title = "Test Application"),
# sidebar ----
dashboardSidebar(
sidebarMenu(id = "sidebarid",
menuItem("Page 1", tabName = "page1"),
menuItem("Page 2", tabName = "page2"),
conditionalPanel(
'input.sidebarid == "page2"',
sliderInput("bins", "Number of bins:", min = 1, max = 50, value = 30),
selectInput("title", "Select plot title:", choices = c("Hist of x", "Histogram of x"))
)
)
),
# body ----
dashboardBody(
tabItems(
# page 1 ----
tabItem(tabName = "page1", "Page 1 content. This page doesn't have any sidebar menu items."),
# page 2 ----
tabItem(tabName = "page2",
"Page 2 content. This page has sidebar meny items that are used in the plot below.",
br(), br(),
plotOutput("distPlot"))
)
)
)
# server -----------------------------------------------------------------------
server <- function(input, output, session) {
output$distPlot <- renderPlot({
x <- faithful[, 2]
bins <- seq(min(x), max(x), length.out = input$bins + 1)
hist(x, breaks = bins, col = "darkgray", border = "white", main = input$title)
})
}
# shiny app --------------------------------------------------------------------
shinyApp(ui, server)
If I understood your question correctly, all you need to do is the following: When defining the dashboardSidebar
if you just want it as a navigational panel add sidebarmenu()
and then add your menu items.如果我正确理解了您的问题,那么您需要做的就是:在定义
dashboardSidebar
时,如果您只想将其用作导航面板,请添加sidebarmenu()
,然后添加您的菜单项。
Then to have the checkboxes and select input appear only for the main dashboard on page 2 add it under dashboardbody()
with the tabItem = "page2"
.然后让复选框和 select 输入仅出现在第 2 页的主仪表板中,将其添加到
dashboardbody()
下,并带有tabItem = "page2"
。 See below:见下文:
#ui.R
library(shiny)
library(shinydashboard)
# Define UI for application that draws a histogram
ui<- dashboardPage(
dashboardHeader(title = "Test Application",titleWidth = "400px"),
dashboardSidebar(
sidebarMenu( #Add sidebarMenu here!!!!!
menuItem("Page 1", tabName = "page1", icon = icon("dashboard")), # You can add icons to your menu if you want
menuItem("Page 2", tabName = "page2", icon = icon("dashboard")))),
dashboardBody(
tabItems(
tabItem(
tabName = "page1",
h1("This is page 1")),
tabItem( #Add checkboxGroupInput into this tabItem
tabName = "page2",
h1("This is page 2"),
checkboxGroupInput("outcome", "Select Outcome Variable(s):", choices = c("Box 1", "Box 2", "Box 3")),
selectInput("selectinput", label = "Select:", choices = c("Choice 1", "Choice 2", "Choice 2")))
)
))
server <- function(input,output,session) {
}
shinyApp(ui, server)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.