-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.R
40 lines (31 loc) · 1.32 KB
/
server.R
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
library(recommenderlab)
library(googlesheets)
shinyServer(function(input, output, session) {
getRecommendations<-function(recom_matrix,allUsers,meth,user,n_recs){
model<-Recommender(recom_matrix,meth)
recom<-predict(model,recom_matrix,n=n_recs)
recommendationList<-as(recom,'list')
return(recommendationList[allUsers==user])
}
getData<-function(){
ss <- gs_url(input$SpreadsheetId, lookup = FALSE, visibility = "public")
GoogleDoc<-gs_read(ss)
GoogleDocMat<-as.matrix(GoogleDoc[,-1])
class(GoogleDocMat)<-"numeric"
r2 <- as(GoogleDocMat, "realRatingMatrix")
return(list(allUsers=GoogleDoc[,2],recommatrix=r2))
}
validUser<-function(data,user){
return(user %in% as.matrix(data$allUsers))
}
Recommend<-eventReactive(input$actionRecommend,{
data<-getData()
if(validUser(data,input$username)){
recoms<-getRecommendations(data$recommatrix, data$allUsers,input$method, input$username, input$n_recoms)
paste("\t",recoms[[1]][1:input$n_recoms],"\n")
}else{"User not found. Be sure to rate some items first."}
})
output$R1<-renderText({
Recommend()
})
})