shinydashboard is a popular R package that provides a framework for creating interactive dashboards in Shiny applications. One of the key elements of shinydashboard is the menuItem function, which allows you to create interactive menu items that can be used to navigate between different sections or pages of your dashboard. In this article, we will explore how to use shinydashboard::menuItem to create interactive menus in your R Shiny applications.
What is menuItem in shinydashboard?
menuItem is a function provided by the shinydashboard package in R that allows you to create interactive menu items in your Shiny applications. These menu items can be used to create navigation menus or sidebars in your dashboard application, allowing users to switch between different sections or pages of the dashboard with just a click.
To get started, you need to load the shinydashboard package in your Shiny application using the library() function:
library(shinydashboard)
Creating Menus with menuItem
The menuItem function takes in several arguments that allow you to customize the appearance and behavior of the menu item. Here’s an example of how you can create a menu item in R Shiny using menuItem:
library(shiny) library(shinydashboard) ui <- dashboardPage( dashboardHeader(title = "My Dashboard"), dashboardSidebar( sidebarMenu( menuItem("Home", tabName = "home", icon = icon("home")), menuItem("Dashboard", tabName = "dashboard", icon = icon("dashboard")), menuItem("Settings", tabName = "settings", icon = icon("gear")) ) ), dashboardBody( tabItems( tabItem(tabName = "home", h2("Welcome to the Home Page!")), tabItem(tabName = "dashboard", h2("Welcome to the Dashboard Page!")), tabItem(tabName = "settings", h2("Welcome to the Settings Page!")) ) ) ) server <- function(input, output) { } shinyApp(ui = ui, server = server)
In the above example, we are using the menuItem function to create three menu items in the sidebarMenu of the shinydashboard. Each menuItem has a label, a tabName, and an icon associated with it. The tabName argument is used to specify the name of the tab that should be displayed when the menu item is clicked. In the dashboardBody, we are using the tabItems and tabItem functions to define the content that should be displayed for each tab.
Customizing Menus with menuItem
menuItem provides several arguments that allow you to customize the appearance and behavior of the menu items. Some of the commonly used arguments are:
- label: The text label displayed on the menu item.
- tabName: The name of the tab that should be displayed when the menu item is clicked.
- icon: The icon displayed next to the menu item, which can be specified using the icon() function from the shiny package.
- badgeLabel: The text label displayed as a badge on the menu item.
- badgeColor: The color of the badge, which can be one of “default”, “primary”, “info”, “success”, “warning”, or “danger”.
- selected: A logical value indicating whether the menu item should be selected by default when the app starts.
- badge: A shortcut argument that allows you to specify both the badgeLabel and badgeColor at once.
You can also customize other aspects of the menu item’s appearance, such as font size, font color, and background color using CSS styling.
Programmatically select menuItem
Sometimes it might be useful to open a menuItem on the server-side. For example, you can add a button that that opens another menuItem. Here’s an example:
library(shiny) library(shinydashboard) ui <- dashboardPage( dashboardHeader(title = "My Dashboard"), dashboardSidebar( sidebarMenu(id = "sidebar", menuItem("Home", tabName = "home", icon = icon("home"), selected = TRUE), menuItem("Dashboard", tabName = "dashboard", icon = icon("dashboard"), selected = FALSE), menuItem("Settings", tabName = "settings", icon = icon("gear"), selected = FALSE) ) ), dashboardBody( tabItems( tabItem(tabName = "home", h2("Welcome to the Home Page!"), actionButton("settings_1", label = "Settings", icon = icon("cog"))), tabItem(tabName = "dashboard", h2("Welcome to the Dashboard Page!"), actionButton("settings_2", label = "Settings", icon = icon("cog"))), tabItem(tabName = "settings", h2("Welcome to the Settings Page!")) ) ) ) server <- function(input, output, session) { observeEvent(c(input$settings_1, input$settings_2), { updateTabItems(session = session, inputId = "sidebar", selected = "settings") }) } shinyApp(ui = ui, server = server)
In the above example, we have added two buttons to the content of the home and dashboard pages which both link to the settings page. We have added an observeEvent function in the server function that listens for a button to be clicked and updates the selected tab using the updateTabItems function from the shinydashboard package.
Conclusion
The shinydashboard::menuItem function provides a convenient way to create interactive menus in your R Shiny applications. You can customize the appearance and behavior of the menu items using the various arguments provided by the menuItem function, and also make them interactive using Shiny’s reactive programming features. By using menuItem, you can create visually appealing and user-friendly menus that enhance the overall usability and interactivity of your Shiny dashboards.