[ 변경 내역 ]
1. 제목 색 변경 (남색)
2. 제목 크기변경 (24point)
3. select box 대신 radioButtons 형식으로 구현
4. 예측하기 버튼 색 변경 (연한하늘색)
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$style(HTML("
body {
background-color: #F0F8FF; /* 완전 연한 하늘색 배경색 */
}
.btn-predict {
background-color: #87CEEB; /* 하늘색 배경색 */
border-color: #87CEEB; /* 하늘색 테두리 색 */
color: white; /* 글자 색상 */
}
.title {
font-size: 24px; /* 제목 글씨 크기 조정 */
color: #0072B2; /* 남색 글자 색상 */
}
"))
),
div(class = "title", "영화 장르 예측기"), # 직접 HTML 태그를 사용하여 제목 정의
sidebarLayout(
sidebarPanel(
radioButtons("age", "연령대", choices = unique(movie$나이), selected = NULL),
radioButtons("gender", "성별", choices = unique(movie$성별), selected = NULL),
radioButtons("job", "직업", choices = unique(movie$직업), selected = NULL),
radioButtons("married", "결혼여부", choices = unique(movie$결혼여부), selected = NULL),
radioButtons("relationship", "이성친구 여부", choices = unique(movie$이성친구), selected = NULL),
actionButton("predict", "예측하기", class = "btn-predict")
),
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)