我遇到以下问题:
当我在tabItem(s)之间切换时,我想要更改侧边栏。
dashboardPage(
dasboardHeader(title=""),
dashboardSidebar(
sidebarMenu(
menuItem("1",tabName="1"),
menuItem("2",tabName="2)
),
#I want this to be displayed when menuItem "1" is clicked
tabsetPanel(
tabPanel("t1",val="t1",
.... some inputs),
tabPanel("t2",val="t2",
.... some inputs)
),
# This to be displayed when menuItem "2" is clicked
selectInput("s1", label="Select"....),
selectInput("s2", label="Select2"...)
)
dashboardBody(
tabItem(tabName="1",
.......
),
tabItem(tabName="2"
........
)
)
)
我在选项卡之间切换时更改了dashboardBody,但我不知道如何更改dashboardSidebar值。在仪表板侧栏中尝试了这个:
conditionalPanel(
condition="tabName='1'",
#displaying first version of DashboardSidebar
),
conditionalPanel(
condition="tabName='2'",
#displaying second version of DashboardSidebar
)
但它对我没用。
有什么想法吗?
感谢您的帮助。
答案 0 :(得分:4)
首先,您必须添加sidebarMenu
一个将用于Shiny输入值的ID,它将报告选择了哪个标签。
之后,将uiOutput
添加到dashboardSidebar
功能。
uiOutput
将根据所选标签接收tabsetPanel
或两个selectInput
。
最后,在服务器端定义的renderUI
内,您只需要创建条件语句。这很容易。
棘手的部分是你必须将selectInput
包装到一个列表中 - 否则只会将第二个小部件发送到UI。更棘手的是,当您想通过ID
将其发送到用户界面时,必须指定tabsetPanel
renderUI
。 (这非常棘手,因为通常你不必指定它的ID
!)
完整示例:
library(shiny)
library(shinydashboard)
rm(ui)
rm(server)
ui <- dashboardPage(
dashboardHeader(title = ""),
dashboardSidebar(
# added ID which will be used for a Shiny input value,
# and it will report which tab is selected.
sidebarMenu(id = "tab",
menuItem("1", tabName = "1"),
menuItem("2", tabName = "2")
),
uiOutput("out1")
),
dashboardBody(
tabItem(tabName = "1"),
tabItem(tabName = "2")
)
)
server <- function(input, output) {
output$out1 <- renderUI({
if (input$tab == "1") {
dyn_ui <- tabsetPanel(id = "tabset_id", selected = "t1",
tabPanel("t1", value = "t1"),
tabPanel("t2", value = "t2"))
}
if (input$tab == "2") {
dyn_ui <- list(selectInput("s1", label = "Select", choices = letters[1:3]),
selectInput("s2", label = "Select2", choices = letters[4:6]))
}
return(dyn_ui)
})
}
shinyApp(ui, server)