R 当选择不同的tabItem时,仪表板侧栏会发生变化
我有以下问题: 当我在tabItem之间切换时,我想更改侧边栏 在选项卡之间切换时,我更改了仪表板主体,但不知道如何更改仪表板侧边栏值。在仪表板侧栏中尝试了以下操作:R 当选择不同的tabItem时,仪表板侧栏会发生变化,r,shiny,shiny-server,shinydashboard,R,Shiny,Shiny Server,Shinydashboard,我有以下问题: 当我在tabItem之间切换时,我想更改侧边栏 在选项卡之间切换时,我更改了仪表板主体,但不知道如何更改仪表板侧边栏值。在仪表板侧栏中尝试了以下操作: conditionalPanel( condition="tabName='1'", #displaying first version of DashboardSidebar ), conditionalPanel( condi
conditionalPanel(
condition="tabName='1'",
#displaying first version of DashboardSidebar
),
conditionalPanel(
condition="tabName='2'",
#displaying second version of DashboardSidebar
)
但这对我不起作用
有什么想法吗
谢谢您的帮助。首先,您必须添加一个ID,该ID将用于一个闪亮的输入值,并报告选择了哪个选项卡 之后,将
uiOutput
添加到dashboardSidebar
函数中
uiOutput
将根据所选选项卡接收tabsetPanel
或两个selectInput
s
最后,在服务器端定义的renderUI
中,只需创建条件语句。这是最简单的部分
棘手的部分是您必须将selectInput
s包装到一个列表中,否则只有第二个小部件会被发送到UI。更棘手的是,当您想通过renderUI
将其发送到UI时,必须指定tabsetPanel
的ID
。(这非常棘手,因为通常不必指定其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)
库(闪亮)
图书馆(shinydashboard)
rm(用户界面)
rm(服务器)
用户界面已经按照您的建议使用侧边栏菜单id解决了这个问题。未使用renderUI,但使用条件面板。非常感谢。你能粘贴代码吗?我会感兴趣的。刚开始玩闪亮的
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)