-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.R
More file actions
106 lines (90 loc) · 2.69 KB
/
server.R
File metadata and controls
106 lines (90 loc) · 2.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
## server.R
# load functions
source('functions/cf_algorithm.R') # collaborative filtering
source('functions/similarity_measures.R') # similarity measures
source('scripts/preprocess.R')
source('scripts/recommender.R')
get_user_ratings <- function(value_list) {
dat <- data.table(movie_id = sapply(strsplit(names(value_list), "_"), function(x) ifelse(length(x) > 1, x[[2]], NA)),
rating = unlist(as.character(value_list)))
dat <- dat[!is.null(rating) & !is.na(movie_id)]
dat[rating == " ", rating := 0]
dat[, ':=' (movie_id = paste("m", movie_id, sep = ""), rating = as.numeric(rating))]
dat <- dat[rating > 0]
idx = which(dat$movie_id %in% movieIDs)
new.ratings = rep(NA, n.item)
new.ratings[idx] = dat$rating
new.ratings
}
shinyServer(function(input, output, session) {
# show the books to be rated
output$selectGenre <- renderUI({
selectInput(
"selectGenre",
"Genre",
genre_list,
selected = NULL
)
})
output$selectSortBy <- renderUI({
radioButtons(
"selectSortBy",
"Sort By",
c("Popularity", "Average Rating")
)
})
output$selectRecommendation <- renderUI({
radioButtons(
"selectRecommendation",
"Recommendation Method",
c("UBCF", "IBCF")
)
})
handleButtonResetRecommendation <- eventReactive(input$btnSubmitRating, {
removeUI("#recomm")
moviesToRate = getRecommendedGenreMovies("", "Popularity")
getMovieRatingTiles(moviesToRate)
})
observeEvent(input$btnResetRecommendation, {
removeUI("#recomm")
# Get personalized recommendations
value_list <- reactiveValuesToList(input)
user_ratings <- get_user_ratings(value_list)
cat(str(input$selectRecommendation))
movies = getRecommendedMovies(user_ratings, input$selectRecommendation)
insertUI(
"#placeholder",
"afterEnd",
ui = div(
id = 'recomm',
box(
width = 12,
title = "We found these movies that you might like",
getMovieTiles(movies)
)
)
)
})
# Calculate recommendations when the sbumbutton is clicked
handleEventGenreFilterChange <- eventReactive(
{
input$selectGenre
input$selectSortBy
},
{
getRecommendedGenreMovies(input$selectGenre, input$selectSortBy)
}
)
# display the recommendations
output$recommendationResults1 <- renderUI({
getMovieTiles(handleEventGenreFilterChange())
})
# show the movies to be rated
output$recommendationResults2 <- renderUI({
box(
width = 12,
title = "Rate these movies to get movie recommendations based on your preference",
handleButtonResetRecommendation()
)
})
}) # server function