Using KPI Titles in Shiny TabPanels

Introduction to Shiny TabPanel with KPI Titles

In this article, we will explore how to create a tabPanel in R Shiny with tab titles that contain Key Performance Indicators (KPIs). We’ll also delve into the necessary packages and techniques required to achieve this goal.

Prerequisites: Setting Up Your Environment

Before diving into the code, ensure you have RStudio installed on your computer. Additionally, install and load the shinydashboard package using the following command:

install.packages("shinydashboard")
library(shinydashboard)

Understanding Shiny Dashboard

The shinydashboard package provides a set of pre-built UI components that can be used to build Shiny applications. It includes various panels, such as tabsetPanel, sidebar, and sideBarLayout.

Creating a Tab Panel with KPI Titles

To create a tab panel with KPI titles, you’ll need to use the tabsetPanel component from the shinydashboard package. This component allows you to create multiple tabs within a single panel.

Here’s an example code snippet that demonstrates how to create a simple tabsetPanel:

library(shinydashboard)
library(dplyr)

# Create a sample dataset for demonstration purposes
df <- data.frame(
  Category = c("A", "B", "C"),
  Value = rnorm(3, mean=10, sd=2),
  Unit = c("tons", "tons", "tons")
)

ui <- fluidPage(
  tabsetPanel(
    tabPanel("Category A",
             infoBox(paste("Average:", round(mean(df$Value), 2)), icon = icon("chart-bar"), fill = TRUE)),
    tabPanel("Category B",
             infoBox(paste("Average:", round(mean(df$Value[df$Category == "B"] , 2)), 2)), icon = icon("table"), fill = TRUE))
)

server <- function(input, output, session) {
  output$plot <- renderPlot({
    hist(
      df$Value,
      main = "R Shiny TabPanel with KPI",
      xlab = ""
    )
  })
}

shinyApp(ui, server)

In the code above:

  1. We first import the necessary libraries.
  2. We create a sample dataset to demonstrate the example.
  3. The ui section of our application includes a tabsetPanel.
  4. Each tab contains an infoBox component with a KPI title and value.
  5. The server-side code is similar to the original Shiny app, but we’ve modified it slightly for demonstration purposes.

Using Icons in Tab Panel Titles

The example above shows how to display icons next to the tab panel titles using the icon function from the shinydashboard package.

Here’s an updated version of the code snippet with additional icon usage:

library(shinydashboard)
library(dplyr)

# Create a sample dataset for demonstration purposes
df <- data.frame(
  Category = c("A", "B", "C"),
  Value = rnorm(3, mean=10, sd=2),
  Unit = c("tons", "tons", "tons")
)

ui <- fluidPage(
  tabsetPanel(
    tabPanel(icon("chart-bar") , paste("Average:", round(mean(df$Value), 2)), fill = TRUE),
    tabPanel(icon("table"), paste("Average:",round(mean(df$Value[df$Category == "B"] , 2))),"icons"))  
)

server <- function(input, output, session) {
  output$plot <- renderPlot({
    hist(
      df$Value,
      main = "R Shiny TabPanel with KPI",
      xlab = ""
    )
  })
}

shinyApp(ui, server)

In the updated code:

  1. We’ve added additional icons to each tab panel using the icon function.
  2. The icons are displayed before the respective KPI titles.

Displaying KPI Titles in Shiny TabPanel

To display KPI titles directly within the tabsetPanel, we can use a combination of textOutput and tabPanel.

Here’s an updated version of the code snippet that demonstrates how to do this:

library(shinydashboard)
library(dplyr)

# Create a sample dataset for demonstration purposes
df <- data.frame(
  Category = c("A", "B", "C"),
  Value = rnorm(3, mean=10, sd=2),
  Unit = c("tons", "tons", "tons")
)

ui <- fluidPage(
  tabsetPanel(
    tabPanel(textOutput("tab_A"), textOutput("tab_B")),
    tabPanel(textOutput("tab_C"))
  ))

server <- function(input, output, session) {
  
  output$tab_A <- renderText({
    paste("Average:", round(mean(df$Value[df$Category == "A"] , 2)),"tons")
  })

  output$tab_B <- renderText({
    paste("Average:",round(mean(df$Value[df$Category == "B"] , 2)),2))
  
}

shinyApp(ui, server)

In the updated code:

  1. We’ve replaced each infoBox component with a single textOutput.
  2. Each textOutput contains a KPI title based on the respective dataset values.

Conclusion

In this article, we explored how to create a tabPanel in R Shiny with tab titles that contain Key Performance Indicators (KPIs). We’ve also discussed various techniques and packages required to achieve this goal. By using the shinydashboard package, you can easily customize your app’s UI components to display KPI data in an attractive way.

The final code snippet provided demonstrates how to use tabsetPanel, textOutput, and other Shiny components together to create a simple yet functional app with KPI titles. Feel free to experiment with the code snippets above to improve your own application’s user experience!


Last modified on 2024-03-22