2024-07-12

Introduction

I am developing an interactive Shiny application that leverages the powerful ggplot2 package to provide dynamic data filtering and visualization using the mpg dataset. This app is designed to help users explore and analyze the mpg dataset by filtering the data based on manufacturer, transmission type, and the number of cylinders. The interactive nature of the app allows users to gain insights quickly and efficiently.

Key Features

  • Dynamic Data Filtering
  • The app updates the displayed data in real-time based on user selections
  • The filtered data is displayed in an interactive table
  • Users can easily sort, search, and explore the data within the table

Server Code

# Load the ggplot2 package which provides the 'mpg' dataset.
library(ggplot2)

function(input, output) {
  # Filter data based on selections
  output$table <- DT::renderDataTable(DT::datatable({
    data <- mpg
    if (input$man != "All") {
      data <- data[data$manufacturer == input$man,]
    }
    if (input$cyl != "All") {
      data <- data[data$cyl == input$cyl,]
    }
    if (input$trans != "All") {
      data <- data[data$trans == input$trans,]
    }
    data
  }))
}

The UI Code

### Load the ggplot2 package which provides the 'mpg' dataset.
library(ggplot2)
fluidPage(titlePanel("Basic DataTable"),
  # Create a new Row in the UI for selectInputs
  fluidRow(column(4,selectInput("man",
  "Manufacturer:", c("All",
  unique(as.character(mpg$manufacturer)))) ),
  column(4, selectInput("trans", "Transmission:",
         c("All",unique(as.character(mpg$trans)))) ),
  column(4, selectInput("cyl","Cylinders:", c("All",
  unique(as.character(mpg$cyl)))))),
  # Create a new row for the table.
  DT::dataTableOutput("table")
)