You can do this using shinyjs
package along with some custom css
. Here is a minimal example:
library(shinydashboard)
library(shinyjs)
ui <- dashboardPage(
dashboardHeader(title = "Basic dashboard"),
dashboardSidebar(
useShinyjs(),
sidebarMenu(id = "sidebar",
tags$head(tags$style(".inactiveLink {
pointer-events: none;
cursor: default;
}")),
menuItem("Dashboard", tabName = "dashboard", icon = icon("dashboard")),
menuItem("Widgets", tabName = "widgets", icon = icon("th"))
)
),
dashboardBody(
tabItems(
# First tab content
tabItem(tabName = "dashboard",
actionButton("Disable", "Disable Widgets"),
actionButton("Enable", "Enable Widgets")
),
# Second tab content
tabItem(tabName = "widgets",
h2("Widgets tab content")
)
)
)
)
server <- function(input, output){
observeEvent(input$Disable, {
addCssClass(selector = "a[data-value='widgets']", class = "inactiveLink")
})
observeEvent(input$Enable, {
removeCssClass(selector = "a[data-value='widgets']", class = "inactiveLink")
})
}
shinyApp(ui, server)
By clicking the button "Enable(Enable Widgets)" and "Disable(Disable Widgets)" you can enable and disable the menuitem widgets.
EDIT:
To ensure that the when the app loads the menuItems
are disabled you can just add the addCssClass
function in your server so that your it gets executed when your app loads.
So the code will look something like this:
library(shinydashboard)
library(shinyjs)
ui <- dashboardPage(
dashboardHeader(title = "Basic dashboard"),
dashboardSidebar(
useShinyjs(),
sidebarMenu(id = "sidebar",
tags$head(tags$style(".inactiveLink {
pointer-events: none;
cursor: default;
}")),
menuItem("Dashboard", tabName = "dashboard", icon = icon("dashboard")),
menuItem("Widgets", tabName = "widgets", icon = icon("th"))
)
),
dashboardBody(
tabItems(
# First tab content
tabItem(tabName = "dashboard",
actionButton("Disable", "Disable Widgets"),
actionButton("Enable", "Enable Widgets")
),
# Second tab content
tabItem(tabName = "widgets",
h2("Widgets tab content")
)
)
)
)
server <- function(input, output){
#Disable menuitem when the app loads
addCssClass(selector = "a[data-value='widgets']", class = "inactiveLink")
observeEvent(input$Disable, {
addCssClass(selector = "a[data-value='widgets']", class = "inactiveLink")
})
observeEvent(input$Enable, {
removeCssClass(selector = "a[data-value='widgets']", class = "inactiveLink")
})
}
shinyApp(ui, server)