Spaces:
Runtime error
Runtime error
Update app.R
Browse files
app.R
CHANGED
|
@@ -1,12 +1,31 @@
|
|
| 1 |
library(shiny)
|
| 2 |
library(randomForest)
|
| 3 |
|
|
|
|
| 4 |
model <- readRDS("final_rf_model.rds")
|
| 5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
ui <- fluidPage(
|
| 7 |
titlePanel("T2D Risk Prediction App"),
|
| 8 |
sidebarLayout(
|
| 9 |
sidebarPanel(
|
|
|
|
| 10 |
numericInput("Age", "Age", 55),
|
| 11 |
numericInput("Weight", "Weight (kg)", 70),
|
| 12 |
numericInput("BMI", "BMI", 25),
|
|
@@ -15,6 +34,8 @@ ui <- fluidPage(
|
|
| 15 |
numericInput("Total.Cholesterol", "Total Cholesterol", 200),
|
| 16 |
numericInput("HDL", "HDL", 50),
|
| 17 |
numericInput("LDL", "LDL", 120),
|
|
|
|
|
|
|
| 18 |
selectInput("Gender", "Gender", choices = c("Female", "Male")),
|
| 19 |
selectInput("CVD", "CVD", choices = c("No", "Yes")),
|
| 20 |
selectInput("Hyperlipidemia", "Hyperlipidemia", choices = c("No", "Yes")),
|
|
@@ -24,6 +45,8 @@ ui <- fluidPage(
|
|
| 24 |
selectInput("Fx.Hyperlipidemia", "Fx.Hyperlipidemia", choices = c("No", "Yes")),
|
| 25 |
selectInput("Smoking.", "Smoking.", choices = c("No", "Yes")),
|
| 26 |
selectInput("Fx.CVD", "Fx.CVD", choices = c("No", "Yes")),
|
|
|
|
|
|
|
| 27 |
selectInput("rs12137794", "rs12137794", choices = c("CC", "TC", "TT")),
|
| 28 |
selectInput("rs587404", "rs587404", choices = c("AA", "AG", "GG")),
|
| 29 |
selectInput("rs1260326", "rs1260326", choices = c("CC", "CT", "TT")),
|
|
@@ -79,23 +102,29 @@ ui <- fluidPage(
|
|
| 79 |
)
|
| 80 |
)
|
| 81 |
|
|
|
|
| 82 |
server <- function(input, output) {
|
| 83 |
observeEvent(input$predict, {
|
| 84 |
-
|
|
|
|
|
|
|
| 85 |
|
| 86 |
-
|
| 87 |
-
|
| 88 |
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
|
|
|
| 92 |
|
| 93 |
-
|
|
|
|
| 94 |
|
| 95 |
output$result <- renderPrint({
|
| 96 |
-
paste("Predicted T2D Risk (Yes = 1 / No = 0):",
|
| 97 |
})
|
| 98 |
})
|
| 99 |
}
|
| 100 |
|
| 101 |
-
|
|
|
|
|
|
| 1 |
library(shiny)
|
| 2 |
library(randomForest)
|
| 3 |
|
| 4 |
+
# Load the saved random forest model
|
| 5 |
model <- readRDS("final_rf_model.rds")
|
| 6 |
|
| 7 |
+
# Extract exactly the factor-levels metadata from the model
|
| 8 |
+
if (!is.null(model$forest$xlevels)) {
|
| 9 |
+
factor_levels <- model$forest$xlevels
|
| 10 |
+
} else if (!is.null(model$xlevels)) {
|
| 11 |
+
factor_levels <- model$xlevels
|
| 12 |
+
} else {
|
| 13 |
+
stop("Cannot find factor-level metadata in model!")
|
| 14 |
+
}
|
| 15 |
+
factor_vars <- names(factor_levels)
|
| 16 |
+
|
| 17 |
+
# Define numeric inputs used in the model
|
| 18 |
+
num_vars <- c(
|
| 19 |
+
"Age", "Weight", "BMI", "Height",
|
| 20 |
+
"Triglyceride", "Total.Cholesterol", "HDL", "LDL"
|
| 21 |
+
)
|
| 22 |
+
|
| 23 |
+
# UI definition
|
| 24 |
ui <- fluidPage(
|
| 25 |
titlePanel("T2D Risk Prediction App"),
|
| 26 |
sidebarLayout(
|
| 27 |
sidebarPanel(
|
| 28 |
+
# Numeric inputs
|
| 29 |
numericInput("Age", "Age", 55),
|
| 30 |
numericInput("Weight", "Weight (kg)", 70),
|
| 31 |
numericInput("BMI", "BMI", 25),
|
|
|
|
| 34 |
numericInput("Total.Cholesterol", "Total Cholesterol", 200),
|
| 35 |
numericInput("HDL", "HDL", 50),
|
| 36 |
numericInput("LDL", "LDL", 120),
|
| 37 |
+
|
| 38 |
+
# Clinical factor inputs
|
| 39 |
selectInput("Gender", "Gender", choices = c("Female", "Male")),
|
| 40 |
selectInput("CVD", "CVD", choices = c("No", "Yes")),
|
| 41 |
selectInput("Hyperlipidemia", "Hyperlipidemia", choices = c("No", "Yes")),
|
|
|
|
| 45 |
selectInput("Fx.Hyperlipidemia", "Fx.Hyperlipidemia", choices = c("No", "Yes")),
|
| 46 |
selectInput("Smoking.", "Smoking.", choices = c("No", "Yes")),
|
| 47 |
selectInput("Fx.CVD", "Fx.CVD", choices = c("No", "Yes")),
|
| 48 |
+
|
| 49 |
+
# Genetic SNP factor inputs
|
| 50 |
selectInput("rs12137794", "rs12137794", choices = c("CC", "TC", "TT")),
|
| 51 |
selectInput("rs587404", "rs587404", choices = c("AA", "AG", "GG")),
|
| 52 |
selectInput("rs1260326", "rs1260326", choices = c("CC", "CT", "TT")),
|
|
|
|
| 102 |
)
|
| 103 |
)
|
| 104 |
|
| 105 |
+
# Server logic
|
| 106 |
server <- function(input, output) {
|
| 107 |
observeEvent(input$predict, {
|
| 108 |
+
# a) gather inputs without deprecated call
|
| 109 |
+
in_list <- reactiveValuesToList(input)
|
| 110 |
+
df <- as.data.frame(in_list, stringsAsFactors = FALSE)
|
| 111 |
|
| 112 |
+
# b) convert numeric columns
|
| 113 |
+
df[num_vars] <- lapply(df[num_vars], as.numeric)
|
| 114 |
|
| 115 |
+
# c) convert factor columns using model's levels
|
| 116 |
+
for (var in factor_vars) {
|
| 117 |
+
df[[var]] <- factor(df[[var]], levels = factor_levels[[var]])
|
| 118 |
+
}
|
| 119 |
|
| 120 |
+
# d) make prediction
|
| 121 |
+
pred <- predict(model, df, type = "response")
|
| 122 |
|
| 123 |
output$result <- renderPrint({
|
| 124 |
+
paste("Predicted T2D Risk (Yes = 1 / No = 0):", pred)
|
| 125 |
})
|
| 126 |
})
|
| 127 |
}
|
| 128 |
|
| 129 |
+
# Run the app
|
| 130 |
+
shinyApp(ui, server, options = list(host = "0.0.0.0", port = 7860))
|