我正在从数据库加载用户和相应的角色。对于某些角色,我希望第二个
menuItem
消失,并且我使用 conditionalPanel
来达到很好的效果。问题是,对于具有正确角色的用户,第二个 menuItem
在加载应用程序后显示,但一旦上面的 menuItem
展开,就会消失。而且它不会回来。
我这样做的方法正确吗?
# global ------------------------------------------------------------------
library(shiny)
library(shinydashboard)
library(shinyWidgets)
library(dplyr)
# header ------------------------------------------------------------------
header <- dashboardHeader(
titleWidth = "300px",
title = "Menuitem disappears"
)
# sidebar -----------------------------------------------------------------
sidebar <- dashboardSidebar(
disable = FALSE,
collapsed = FALSE,
width = "300px",
sidebarMenu(
id = "menu1",
menuItem(
"Menu Item 1",
menuSubItem("Submenu Item 1", tabName = "tab_1"),
menuSubItem("Submenu Item 2", tabName = "tab_2"),
tabName = "tab_3"
),
conditionalPanel(
condition = "output.performance",
sidebarMenu(
menuItem(
"Menu Item 2",
menuSubItem("Submenu Item 1", tabName = "tab_5"),
tabName = "tab_4"
)
)
)
),
hr(),
pickerInput(
inputId = "myinput",
label = "Example Input",
choices = c("A", "B", "C"),
inline = FALSE,
width = "100%",
options = list(showTick = FALSE),
)
)
# body --------------------------------------------------------------------
body <- dashboardBody(
"Text goes here"
)
ui <- dashboardPage(header, sidebar, body, skin = "black", title = "Reprex")
server <- function(input, output, session) {
user <- reactive({
"example user"
})
user_roles <- reactive({
"Boss"
})
output$performance <- reactive({
any(c("Boss") %in% user_roles())
})
outputOptions(output, "performance", suspendWhenHidden = FALSE)
}
shinyApp(ui, server)
我找到了解决办法。我认为正确的方法是使用
renderMenu
:
# global ------------------------------------------------------------------
library(shiny)
library(shinydashboard)
library(shinyWidgets)
library(dplyr)
# header ------------------------------------------------------------------
header <- dashboardHeader(
titleWidth = "300px",
title = "Menuitem disappears"
)
# sidebar -----------------------------------------------------------------
sidebar <- dashboardSidebar(
disable = FALSE,
collapsed = FALSE,
width = "300px",
sidebarMenuOutput("myMenu"),
hr(), ## TMP
pickerInput(
inputId = "myinput",
label = "Example Input",
choices = c("A", "B", "C"),
inline = FALSE,
width = "100%",
options = list(showTick = FALSE),
)
)
# body --------------------------------------------------------------------
body <- dashboardBody(
"Text goes here"
)
ui <- dashboardPage(header, sidebar, body, skin = "black", title = "Reprex")
server <- function(input, output, session) {
user <- reactive({
"example user"
})
user_roles <- reactive({
"Boss"
})
output$myMenu <- renderMenu({
menu_list <- list(
menuItem(
"Menu Item 1",
menuSubItem("Submenu Item 1", tabName = "tab_1"),
menuSubItem("Submenu Item 2", tabName = "tab_2"),
tabName = "tab_3"
)
)
if (any(c("Boss") %in% user_roles())) {
menu_list <- append(
menu_list,
list(
menuItem(
"Menu Item 2",
menuSubItem("Submenu Item 1", tabName = "tab_5"),
tabName = "tab_4"
)
)
)
}
sidebarMenu(
id = "menu1",
.list = menu_list
)
})
}
shinyApp(ui, server)