交叉发布到RStudio社区。
我正在开发一个闪亮的仪表板,并尝试让右侧边栏根据扩展的左侧边栏菜单项来显示不同的内容。
问题是:第一次切换menuItems后,只有当您第二次单击当前展开的menuItem将其关闭,然后再单击展开其他menuItem时,右侧边栏中的内容才会发生变化。
如果您只是从 menuItem 1 单击到 menuItem 2,则当另一个 menuItem 展开时,第一个 menuItem 会在视觉上缩小,但显然 input$sidebarItemExpanded 的值不会更改,直到您手动“取消选择”第一个 menuItem。这对于用户来说确实不直观,所以我正在寻找一种方法来解决它。
这是一个示例应用程序:
library(shiny)
# convertMenuItem function, from https://stackoverflow.com/questions/31794702/r-shiny-dashboard-tabitems-not-apparing
convertMenuItem <- function(mi,tabName) {
mi$children[[1]]$attribs['data-toggle']="tab"
mi$children[[1]]$attribs['data-value'] = tabName
mi
}
ui <- tags$body(class = "skin-blue sidebar-mini control-sidebar-open", tagList(dashboardPagePlus(
header = dashboardHeaderPlus(
title = "Animals",
enable_rightsidebar = TRUE,
rightSidebarIcon = "paw"
),
body = dashboardBody(),
rightsidebar = rightSidebar(
uiOutput("rightSidebar") # We'll define this dynamically using renderUI in the server
),
## Left sidebar
sidebar = dashboardSidebar(
sidebarMenu(
# Cats menu item
convertMenuItem(menuItem(expandedName = "cats", text = "Cats",
tabName = "cats", icon = icon("cat"),
startExpanded = TRUE,
class = "active",
h4("CAT CONTENT"),
selectInput("catSelector", label = "Select a cat",
choices = c("American Shorthair", "Ragdoll",
"Bengal", "Siamese", "Sphynx"))
), tabName = "cats"
),
convertMenuItem(menuItem(expandedName = "dogs", text = "Dogs",
tabName = "dogs", icon = icon("dog"),
class = "active",
h4("DOG CONTENT"),
selectInput("dogSelector", label = "Select a dog",
choices = c("German Shepherd", "Poodle",
"Chihuahua", "Husky", "Pitbull"))
), tabName = "dogs"
)
)
)
),
)
)
server <- function(input, output, session){
# RIGHT MENU BAR CONTROLS -------------------------------------------------
observeEvent(input$sidebarItemExpanded, {
if(input$sidebarItemExpanded == "cats"){
message("Cats view has been selected")
output$rightSidebar <- renderUI({
rightSidebar(
### Right sidebar cat content
rightSidebarTabContent(
title = "Cat stuff",
id = "catStuff",
active = T,
icon = "cat",
p("Here is some information about cats.")
)
)
})
}else if(input$sidebarItemExpanded == "dogs"){
message("Dogs view has been selected")
output$rightSidebar <- renderUI({
rightSidebar(
### Right sidebar dog content
rightSidebarTabContent(
title = "Dog stuff",
id = "dogStuff",
active = T,
icon = "dog",
p("Here is some information about dogs.")
)
)
})
}
})
}
shinyApp(ui = ui, server = server)
如果有帮助,这里是我演示问题的屏幕录制。您可以看到应用程序打开,“猫”选项卡展开,并且右侧边栏中有与猫相关的内容。然后,如果我单击“狗”选项卡,右侧边栏中的内容会相应更新。但是,如果我尝试返回“猫”选项卡,右侧边栏内容不会更新,除非我再次单击“狗”选项卡以“关闭”它,即使它看起来已经对用户。
我已经在shinydashboard文档中阅读了几篇关于childfull menuItems的行为、如何使用input$sidebarItemExpanded等的帖子和信息,但我发现这一切都非常令人困惑。我的问题似乎与here记录的内容有些相似,除了在该示例中,childfull 选项卡所需的内容根本不会显示。就我而言,内容才会显示,但前提是您手动“关闭”显然已展开的另一个选项卡,尽管它看起来并非如此。用户 ismirsehregal 提供了涉及隐藏菜单项的问题的答案,但我不确定如何实现它来更改右侧边栏中的内容而不是仪表板主体中的内容。
如果您对此有任何指示,我将不胜感激 - 非常感谢!
你有没有弄清楚这一点?我正在处理同样的问题。