Background
I am trying to develop an app, which has a login screen and shows different menus depending on the user's rights.
The user rights are hierarchical, meaning that an Read + Write user should have the same rights as a Read user plus some additional rights.
I am struggling now how to create this dynamic menu. I read this document on dynamic UIs and I hoped I could achieve my goal purely with the use of conditionalPanel calls.
In the code below, you see that there is a lot of duplication. And worst of all, I have to assign unique names to the tabs even if they are in different (mutual exclusive) panels (a1 and a2) but which are meant to do exactly the same.
Things tried so far
I tried to move the conditionalPanel into sidebarMenu (and tabItems respectively), but then I get an error, because the return type of conditionalPanel does not match the expected type.
Expected Outcome
Finally, I want to have minimal duplication of code. That is, I want to define the UI for Do A and Do B exactly once, but re-use Do A for both users.
Which options do I have? Do I have to fall back to dynamic rendering (renderUI / renderMenu) or is there a way how I can achieve the desired behavior w/ conditionalPanel only?
Working Code with Duplication
library(shiny)
library(shinydashboard)
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(
selectInput("userrights", label = "User Rights:",
choices = c("Read + Write", "Read")),
conditionalPanel("input.userrights == 'Read'",
sidebarMenu(
menuItem("Do A", tabName = "a1")
)
),
conditionalPanel("input.userrights == 'Read + Write'",
sidebarMenu(
## unnecessary duplication + I need yet another tab 'a2'
menuItem("Do A", tabName = "a2"),
menuItem("Do B", tabName = "b2")
)
)
),
dashboardBody(
conditionalPanel("input.userrights == 'Read'",
tabItems(
tabItem("a1", h1("A was done"))
)
),
conditionalPanel("input.userrights == 'Read + Write'",
tabItems(
tabItem("a2", h1("A was done")),
tabItem("b2", h1("B was done"))
)
)
)
)
server <- function(input, output) {}
runApp(shinyApp(ui, server))
Snippet: Not working but at least w/o Duplication
sidebarMenu(
menuItem("Do A", tabName = "a")
conditionalPanel("input.userrights == 'Read + Write'",
menuItem("Do B", tabName = "b")
)
)