This is my code.:
# Load required libraries
library(shiny)
library(shinydashboard)
library(ggplot2)
library(caret)
library(glmnet)
library(pROC)
library(conflicted)
library(readxl)
library(rsconnect)
# Set preference for shinydashboard::box to avoid conflict
conflicts_prefer(shinydashboard::box)
Cancer_Data <- read_excel("C:/Users/NITRO 5/OneDrive/Documents/Cancer _predict/Cancer_Data.xlsx")
# UI layout
ui <- dashboardPage(
dashboardHeader(title = "Cancer Diagnosis Prediction"),
dashboardSidebar(
sidebarMenu(
menuItem("Model Performance", tabName = "performance", icon = icon("chart-line")),
menuItem("Prediction", tabName = "prediction", icon = icon("diagnoses"))
)
),
dashboardBody(
tabItems(
tabItem(
tabName = "performance",
fluidRow(
shinydashboard::box(plotOutput("rocPlot"), title = "ROC Curve"),
shinydashboard::box(tableOutput("metricsTable"), title = "Model Metrics")
)
),
tabItem(
tabName = "prediction",
fluidRow(
shinydashboard::box(
title = "Enter Predictor Values",
# Add inputs for each predictor variable
uiOutput("inputFields"),
actionButton("predictBtn", "Predict Diagnosis")
),
shinydashboard::box(
title = "Prediction Result",
textOutput("predictionResult")
)
)
)
)
)
)
# Server logic
server <- function(input, output, session) {
# Preprocess data
train_data <- reactive({
req(Cancer_Data) # Ensure Cancer_Data is loaded
set.seed(125)
trainIndex <- createDataPartition(Cancer_Data$diagnosis, p = 0.75, list = FALSE)
train_data <- Cancer_Data[trainIndex, ]
train_data <- na.omit(train_data)
train_data$diagnosis <- as.factor(train_data$diagnosis)
return(train_data)
})
# Train models
fit_glm <- reactive({
req(train_data()) # Ensure train_data is available
train(diagnosis ~ ., data = train_data(), method = "glm",
metric = "ROC", preProc = c("center", "scale"),
trControl = trainControl(method = "cv", classProbs = TRUE, summaryFunction = twoClassSummary))
})
fit_glmnet <- reactive({
req(train_data()) # Ensure train_data is available
train(diagnosis ~ ., data = train_data(), method = "glmnet",
metric = "ROC", preProc = c("center", "scale"),
trControl = trainControl(method = "cv", classProbs = TRUE, summaryFunction = twoClassSummary))
})
# Plot ROC Curve
output$rocPlot <- renderPlot({
req(fit_glm(), fit_glmnet()) # Ensure models are trained
glm_roc <- roc(response = train_data()$diagnosis, predictor = predict(fit_glm(), train_data(), type = "prob")[,2])
glmnet_roc <- roc(response = train_data()$diagnosis, predictor = predict(fit_glmnet(), train_data(), type = "prob")[,2])
plot(glm_roc, col = "blue", main = "ROC Curves for Models")
lines(glmnet_roc, col = "red")
legend("bottomright", legend = c("GLM", "GLMNET"), col = c("blue", "red"), lty = 1)
})
# Display Sensitivity and Specificity
output$metricsTable <- renderTable({
req(fit_glm(), fit_glmnet()) # Ensure models are trained
glm_conf <- confusionMatrix(predict(fit_glm(), train_data()), train_data()$diagnosis)
glmnet_conf <- confusionMatrix(predict(fit_glmnet(), train_data()), train_data()$diagnosis)
data.frame(
Model = c("GLM", "GLMNET"),
Sensitivity = c(glm_conf$byClass["Sensitivity"], glmnet_conf$byClass["Sensitivity"]),
Specificity = c(glm_conf$byClass["Specificity"], glmnet_conf$byClass["Specificity"])
)
})
# Generate input fields for predictors in Tab 2
output$inputFields <- renderUI({
req(train_data()) # Ensure train_data is available
input_list <- lapply(names(train_data())[-which(names(train_data()) == "diagnosis")], function(var) {
numericInput(inputId = var, label = var, value = 0)
})
do.call(tagList, input_list)
})
# Prediction based on user input
output$predictionResult <- renderText({
req(input$predictBtn)
req(fit_glmnet()) # Ensure model is trained
# Gather input data for prediction
input_data <- as.data.frame(t(sapply(names(train_data())[-which(names(train_data()) == "diagnosis")],
function(var) input[[var]])))
colnames(input_data) <- names(train_data())[-which(names(train_data()) == "diagnosis")]
# Predict diagnosis
prediction <- predict(fit_glmnet(), input_data)
paste("Predicted Diagnosis:", prediction)
})
}
# Run the app
shinyApp(ui, server)
Example data:
cancer_data <- data.frame(
diagnosis = c("M", "M", "M", "B", "B"),
radius_mean = c(17.99, 20.57, 19.69, 11.42, 13.85),
texture_mean = c(10.38, 17.77, 21.25, 20.38, 16.54),
perimeter_mean = c(122.8, 132.9, 130.0, 77.58, 86.49),
area_mean = c(1001, 1326, 1203, 386.1, 403.5),
smoothness_mean = c(0.1184, 0.08474, 0.1096, 0.1425, 0.1003),
compactness_mean = c(0.2776, 0.07864, 0.1599, 0.2839, 0.1328)
)