library(shiny)
library(naivebayes)
library(e1071)
library(caret)
# 데이터 불러오기
movie <- read.csv("c:\\datafile\\movie2.csv", stringsAsFactors=TRUE, fileEncoding = "euc-kr")
# 결측치 확인
colSums(is.na(movie))
# 훈련 데이터와 테스트 데이터로 분리
set.seed(1)
k <- createDataPartition(movie$장르, p=0.8, list=F)
train_data <- movie[k, ]
test_data <- movie[-k, ]
# 나이브 베이즈 모델 생성
new_model <- naive_bayes(장르 ~ ., data=train_data)
# UI 정의
ui <- fluidPage(
titlePanel("영화 장르 예측기"),
tags$head(
tags$style(HTML("
body {
background-color: #DFF0D8; /* Light green background */
font-family: 'Arial', sans-serif; /* Choose a readable font */
}
.sidebar {
background-color: #87CEEB; /* Sidebar background color */
padding: 15px; /* Add padding to sidebar */
}
.main-panel {
padding: 20px; /* Add padding to main panel */
}
.btn-primary {
background-color: #4CAF50; /* Green button color */
border-color: #4CAF50; /* Matching border color */
}
.btn-primary:hover {
background-color: #45a049; /* Darker green on hover */
border-color: #45a049;
}
"))
),
sidebarLayout(
sidebarPanel(
selectInput("age", "나이", choices = unique(movie$나이)),
selectInput("gender", "성별", choices = unique(movie$성별)),
selectInput("job", "직업", choices = unique(movie$직업)),
selectInput("married", "결혼여부", choices = unique(movie$결혼여부)),
selectInput("relationship", "이성친구 여부", choices = unique(movie$이성친구)),
actionButton("predict", "예측하기", class = "btn-primary") # Apply green button style
),
mainPanel(
verbatimTextOutput("prediction")
)
)
)
# 서버 로직 정의
server <- function(input, output) {
observeEvent(input$predict, {
test_data <- data.frame(
나이 = input$age,
성별 = input$gender,
직업 = input$job,
결혼여부 = input$married,
이성친구 = input$relationship
)
result <- predict(new_model, test_data, type = "prob")
output$prediction <- renderPrint({
result
})
})
}
# Shiny 앱 실행
shinyApp(ui = ui, server = server)