library(shiny)
library(ggplot2)


df <- data.frame(
  Features = c(5, 10, 15, 20, 5, 10, 15, 20, 5, 10, 15, 20,5, 10, 15, 20,5, 10, 15, 20),
  Model = c("LDA", "LDA", "LDA", "LDA", 
            "QDA", "QDA", "QDA", "QDA",
            "RANDOM FOREST", "RANDOM FOREST","RANDOM FOREST","RANDOM FOREST",
            "SVM(Linear)","SVM(Linear)","SVM(Linear)","SVM(Linear)",
            "SVM(Radial)","SVM(Radial)","SVM(Radial)","SVM(Radial)"
            ),
  Performance = c(0.73,0.80,0.80,0.73,0.73,0.80,0.60,0.67,0.87,
0.67,0.80,0.67,0.73,0.80,0.80,0.73,0.73,0.73,0.67,0.67),
  Specificity = c(0.71,0.86,0.86,0.71,0.86,0.86,0.71,0.86,1.00,
0.71,0.86,0.71,0.86,0.86,0.86,0.71,0.71,0.86,0.86,0.86),
  Sensitivity = c(0.75,0.75,0.75,0.75,0.63,0.75,0.50,0.50,0.75,
0.63,0.75,0.63,0.63,0.75,0.75,0.75,0.75,0.63,0.50,0.50)
)

# Define UI
ui <- fluidPage(
  titlePanel("Model Comparison: Performance, Specificity, Sensitivity"),
  
  sidebarLayout(
    sidebarPanel(
      selectInput("metric", "Select Metric", choices = c("Performance", "Specificity", "Sensitivity"), selected = "Performance")
    ),
    
    mainPanel(
      plotOutput("comparisonPlot")
    )
  )
)

# Define Server
server <- function(input, output) {
  output$comparisonPlot <- renderPlot({
    # Generate bar plot comparing all models
    ggplot(df, aes(x = factor(Features), y = get(input$metric), fill = Model)) +
      geom_bar(stat = "identity", position = "dodge") +  # Grouped bar chart
      geom_text(aes(label = round(get(input$metric), 2)), position = position_dodge(width = 0.9), vjust = -0.5, size = 4) +  # Labels
      labs(
        title = paste(input$metric, "Comparison Across Models"),
        x = "Top N Features Chosen by Random Forest",
        y = input$metric
      ) +
      theme_minimal() +
      scale_fill_brewer(palette = "Set2")  # Color scheme
  })
}

# Run the application
shinyApp(ui = ui, server = server)
Shiny applications not supported in static R Markdown documents