install.packages(c(“shiny”, “shinydashboard”, “dplyr”, “plotly”, “ggplot2”))
library(shiny) library(shinydashboard) library(dplyr) library(plotly) library(ggplot2)
ui <- dashboardPage( dashboardHeader(title = “Diamond Pricing Dashboard”), dashboardSidebar( sidebarMenu( menuItem(“Overview”, tabName = “overview”, icon = icon(“dashboard”)), menuItem(“Diamond Analysis”, tabName = “analysis”, icon = icon(“gem”)) ) ), dashboardBody( tabItems( tabItem( tabName = “overview”, fluidRow( infoBoxOutput(“avg_carat”, width = 4), infoBoxOutput(“avg_price”, width = 4), infoBoxOutput(“avg_cut”, width = 4) ), fluidRow( box( title = “Diamond Price Distribution”, plotlyOutput(“price_dist_plot”), width = 12 ) ) ), tabItem( tabName = “analysis”, fluidRow( box( title = “Select Diamond Cut”, selectInput(“cut_select”, “Select Diamond Cut”, choices = unique(diamonds$cut), selected = “Ideal”), width = 6 ), box( title = “Price vs Carat”, plotlyOutput(“price_carat_plot”), width = 6 ) ) ) ) ) )
server <- function(input, output) {
# Prepare the diamonds dataset data <- diamonds
# KPI Calculations output\(avg_carat <- renderInfoBox({ avg_carat <- mean(data\)carat) infoBox(“Average Carat”, round(avg_carat, 2), icon = icon(“gem”), color = “blue”) })
output\(avg_price <- renderInfoBox({ avg_price <- mean(data\)price) infoBox(“Average Price”, paste(“$”, format(round(avg_price, 2), big.mark = “,”)), icon = icon(“dollar-sign”), color = “green”) })
output\(avg_cut <- renderInfoBox({ avg_cut <- as.character(names(sort(table(data\)cut), decreasing = TRUE))[1]) infoBox(“Most Common Cut”, avg_cut, icon = icon(“gem”), color = “orange”) })
# Diamond Price Distribution Plot output$price_dist_plot <- renderPlotly({ plot_ly(data, x = ~price, color = ~cut, type = “histogram”, nbinsx = 30, opacity = 0.7) %>% layout(title = “Diamond Price Distribution by Cut”, xaxis = list(title = “Price”), yaxis = list(title = “Count”)) })
# Filtered Data for Selected Diamond Cut filtered_data <- reactive({ data %>% filter(cut == input$cut_select) })
# Price vs Carat Plot for Selected Cut output$price_carat_plot <-
renderPlotly({ plot_ly(filtered_data(), x = ~carat, y = ~price, color =
~cut, type = “scatter”, mode = “markers”, hoverinfo = “text”, text =
~paste(“Carat:”, carat, “
Price: \(",
price)) %>%
layout(title = paste("Price vs Carat for",
input\)cut_select), xaxis = list(title =”Carat”), yaxis =
list(title = “Price”)) }) }
tabItem( tabName = “overview”,
# 📘 Add a descriptive text box at the top fluidRow( box( title = “About This Dashboard”, status = “primary”, solidHeader = TRUE, width = 12, collapsible = TRUE, p(“💎 This dashboard provides an interactive exploration of the diamonds dataset.”), p(“Use the ‘Diamond Analysis’ tab to filter by diamond cut and examine how carat influences price.”), p(“Plots are interactive—hover over points for details or zoom in for closer inspection.”) ) ),
# 💡 KPI InfoBoxes fluidRow( infoBoxOutput(“avg_carat”, width = 4), infoBoxOutput(“avg_price”, width = 4), infoBoxOutput(“avg_cut”, width = 4) ),
# 📊 Price Distribution Plot fluidRow( box( title = “Diamond Price Distribution”, plotlyOutput(“price_dist_plot”), width = 12 ) ) )