title: "Iris Species Visualization" runtime: shiny output: flexdashboard::flexdashboard: orientation: rows social: menu sourcecode: embed theme: simplex
```{r setup , include=FALSE} library(shiny) library(plotly) library(leaflet) library(tidyverse) library(sf) library(highcharter) library(kableExtra) df <- datasets::iris
lookup = structure(c("setosa","versicolor","virginica")) lookup2 = structure(c("Sepal.Length","Sepal.Width","Petal.Length","Petal.Width"))
```
```{r} tags$br()
tags$h4("Customize Scatter Plot and Clustering Settings")
tags$p("Select the variables for the X and Y axes to visualize relationships in the dataset.")
selectInput( 'xcol', label = 'X-axis variable (e.g., Sepal Length):', choices = lookup2, selected = "Sepal.Length" ) selectInput( 'ycol', label = 'Y-axis variable (e.g., Sepal Width):', choices = lookup2, selected = "Sepal.Width" )
tags$hr() tags$p("Click the barplots tab to see different bar plots. You can also visualize K-means for different values of K by going to K-means tab")
tags$p("Use the slider below to select the number of clusters (k) for k-means clustering. The data points will be grouped into k clusters.")
sliderInput( "k", "Number of Clusters (k):", min = 2, max = 10, value
= 3, step = 1
) tags$p("Hover over the data points to know their values.") tags$br()
tags$p("Author: ", tags$span("Ishan Gupta", style = "color: red;"))
```
```{r}
output$scatter <- renderPlotly({
# Dynamically select X and Y columns for ggplot X <- input$xcol Y <- input$ycol
# Create the ggplot scatter plot using aes() for dynamic variables p <- ggplot(df, aesstring(x = X, y = Y , col = df$Species)) + geompoint() + labs(x = X, y = Y) + theme_minimal()
# Convert ggplot object to Plotly interactive plot ggplotly(p) })
plotlyOutput('scatter', width = "100%")
Row {data-height = 200} ----------------------------------------------------------------------- ### Species(Quantile){r}
output$stats <- renderTable({ df %>% groupby(Species) %>%
summarise( MeanX = mean(.data[[input$xcol]]), MedianX =
median(.data[[input$xcol]]), SDX = sd(.data[[input$xcol]]),
MeanY = mean(.data[[input$ycol]]), MedianY =
median(.data[[input$ycol]]), SD_Y = sd(.data[[input$ycol]]) ) })
tableOutput('stats')
``` Bar Plots ====================================================================== Row -----------------------------------------------------------------------
```{r} p <- ggplot(df, aes(x = Species, y = Sepal.Length, fill = Species)) + geomboxplot() + thememinimal()
ggplotly(p) ```
```{r} p <- ggplot(df, aes(x = Species, y = Sepal.Width, fill = Species)) + geomboxplot() + thememinimal()
ggplotly(p) ```
```{r} p <- ggplot(df, aes(x = Species, y = Petal.Length, fill = Species)) + geomboxplot() + thememinimal()
ggplotly(p) ```
```{r} p <- ggplot(df, aes(x = Species, y = Petal.Width, fill = Species)) + geomboxplot() + thememinimal()
ggplotly(p)
```
```{r}
output$clusterPlot <- renderPlotly({ # Perform K-means clustering k <- input$k xcol <- input$xcol ycol <- input$ycol
irisdata <- iris[, 1:4] kmeansresult <- kmeans(irisdata, centers = k, nstart = 25) iris$Cluster <- as.factor(kmeansresult$cluster)
# Create ggplot scatter plot plot <- ggplot(iris, aesstring(x = xcol, y = ycol, color = "Cluster")) + geompoint(size = 3, alpha = 0.7) + theme_minimal() + labs(title = paste("K-means Clustering (k =", k, ")"), x = xcol, y = ycol)
ggplotly(plot) })
plotlyOutput("clusterPlot")
```
```{r} output$summary <- renderUI({ k <- input$k
irisdata <- iris[, 1:4] kmeansresult <- kmeans(irisdata, centers = k, nstart = 25) iris$Cluster <- as.factor(kmeansresult$cluster)
# Confusion matrix (True labels vs Predicted labels) confusion <- table(TrueLabel = iris$Species, PredictedCluster = iris$Cluster)
# Generate and style the table using kableExtra tablehtml <- knitr::kable(confusion, format = "html", table.attr = "class='table table-bordered table-hover'") %>% kableExtra::kablestyling( bootstrapoptions = c("striped", "hover", "condensed"), fullwidth = FALSE, # Use full width for the table position = "center", # Center the table fontsize = 18 # Increase font size for better readability ) %>% kableExtra::columnspec(1, color = "white", background = "#007bff") %>% # Customize the first column kableExtra::column_spec(2:ncol(confusion), background = "#f8f9fa", color = "black") # Customize other columns
# Return the HTML output correctly HTML(table_html) # Use HTML() to render the table as HTML })
uiOutput("summary")
```