if (!require("shiny")) install.packages("shiny")
if (!require("naivebayes")) install.packages("naivebayes")
if (!require("e1071")) install.packages("e1071")
if (!require("caret")) install.packages("caret")
library(shiny)
library(naivebayes)
library(e1071)
library(caret)
# 데이터 불러오기
movie <- read.csv("c:\\data\\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(
tags$head(
tags$link(rel = "stylesheet", href = "https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap"),
tags$style(HTML("
body {
background: url('https://example.com/movie-theater-background.jpg') no-repeat center center fixed;
background-size: cover;
font-family: 'Roboto', sans-serif;
color: #ffffff;
}
.title {
color: #ff6347;
font-family: 'Roboto', sans-serif;
text-shadow: 2px 2px 4px #000000;
}
.sidebar {
background-color: rgba(0, 0, 0, 0.7);
padding: 20px;
border-radius: 5px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);
}
.main {
background-color: rgba(0, 0, 0, 0.7);
padding: 20px;
border-radius: 5px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);
font-size: 1.2em;
line-height: 1.5em;
}
.highlight {
color: #ff4500;
font-weight: bold;
}
"))
),
titlePanel(h1("영화 장르 예측기", class = "title")),
sidebarLayout(
sidebarPanel(
class = "sidebar",
selectInput("age", "나이", choices = c("10대", "20대", "30대", "40대")),
selectInput("gender", "성별", choices = unique(movie$성별)),
selectInput("job", "직업", choices = sort(unique(movie$직업))),
selectInput("married", "결혼여부", choices = c("YES", "NO")),
selectInput("relationship", "이성친구 여부", choices = unique(movie$이성친구)),
actionButton("predict", "예측하기"),
actionButton("reset", "리셋")
),
mainPanel(
class = "main",
uiOutput("prediction")
)
)
)
# 서버 로직 정의
server <- function(input, output, session) {
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")
max_prob <- max(result)
output$prediction <- renderUI({
tags$div(
lapply(1:ncol(result), function(i) {
value <- result[, i]
name <- colnames(result)[i]
class <- ifelse(value == max_prob, "highlight", "")
tags$div(class = class, sprintf("%s: %.4f", name, value))
})
)
})
})
observeEvent(input$reset, {
updateSelectInput(session, "age", selected = "")
updateSelectInput(session, "gender", selected = "")
updateSelectInput(session, "job", selected = "")
updateSelectInput(session, "married", selected = "")
updateSelectInput(session, "relationship", selected = "")
output$prediction <- renderUI({
NULL
})
})
}
# Shiny 앱 실행
shinyApp(ui = ui, server = server)