Skip to content

Instantly share code, notes, and snippets.

@Ray901
Last active August 29, 2015 14:20
Show Gist options
  • Save Ray901/fc89b95ebe68f39836ec to your computer and use it in GitHub Desktop.
Save Ray901/fc89b95ebe68f39836ec to your computer and use it in GitHub Desktop.
R shiny app for EHanlin userExam
library(shiny)
library(dplyr)
library(rmongodb)
library(googleVis)
library(rPython)
sethost<-""
setdb_platform<-""
setdb_exam<-""
setdb_sim<-""
setdbUser<-""
setdbPW<-""
########################################################
source(paste0(getwd(),"/commonFunctions.R"))
########################################################
shinyServer(function(input, output) {
connection_platform <- reactive({
mongo <- mongo.create(host=sethost,db=setdb_platform,username = setdbUser,password = setdbPW)
})
getUserExamAllDat <- reactive({
startDate<-as.numeric(unlist(strsplit(as.character(input$examDates[1]),split="-")))
startDate<-c(startDate,0,0,0)
endDate<-as.numeric(unlist(strsplit(as.character(input$examDates[2]),split="-")))
endDate<-c(endDate,23,59,59)
python.assign("startDate",startDate)
python.assign("endDate",endDate)
if (input$year=="103") {
python.load(paste0(getwd(),"/userExam103.py"))
} else if (input$year=="102") {
python.load(paste0(getwd(),"/userExam102.py"))
} else if (input$year=="103(高)") {
python.load(paste0(getwd(),"/userExam103H.py"))
} else if (input$year=="102(高)") {
python.load(paste0(getwd(),"/userExam102H.py"))
}
result<-python.get("result")
result<-result[[2]]
setcolnames<-names(result[[1]])
A<-as.data.frame(matrix(unlist(result),nrow = length(result),byrow = T),stringsAsFactors = F)
names(A)<-setcolnames
names(A)[which(names(A)=="_id")]<-"user"
A<-A[,match(names(A),c("user","totalcount","rightcount"))]
A$totalcount<-as.numeric(A$totalcount)
A$rightcount<-as.numeric(A$rightcount)
A<-A[order(A$totalcount,decreasing = T),]
return(A)
})
getInputUserId <- reactive({
mongo <-connection_platform()
collections_name<-mongo.get.database.collections(mongo,setdb_platform)
index_collection<-which(collections_name==paste(setdb_platform,".Users",sep=""))
if (input$userchoice=="email") {
query<-list('email'=input$inputUserMailID)
} else if (input$userchoice=="ID") {
query<-list('_id'=mongo.oid.from.string(input$inputUserMailID))
}
count<-mongo.count(mongo,collections_name[index_collection],query)
if (count>0) {
cursor<- mongo.find(mongo,collections_name[index_collection],query)
while (mongo.cursor.next(cursor)) {
Users.id<-mongo.oid.to.string(mongo.bson.to.list(mongo.cursor.value(cursor))$'_id')
}
} else {
Users.id<-NA
}
return(Users.id)
})
getUserExamDat <- reactive({
setUser<-getInputUserId()
if (!is.na(setUser)) {
mongo <-mongo.create(host=sethost)
collections_name<-mongo.get.database.collections(mongo,"ehanlin_102_jhs_db")
index_collection<-which(collections_name==paste("ehanlin_102_jhs_db",".ExamAnswers",sep=""))
count<-mongo.count(mongo,collections_name[index_collection],list(user=setUser,enabled=TRUE,finished=TRUE))
if (count>0) {
cursor<- mongo.find(mongo,collections_name[index_collection],list(user=setUser,enabled=TRUE,finished=TRUE),limit=count)
ExamAnswers.user<-rep(NA,count)
ExamAnswers.subject<-rep(NA,count)
ExamAnswers.volume<-rep(NA,count)
ExamAnswers.questionCount<-rep(NA,count)
ExamAnswers.rightCount<-rep(NA,count)
ExamAnswers.finishDate<-rep(NA,count)
ExamAnswers.type<-rep(NA,count)
i<-0
while (mongo.cursor.next(cursor)) {
i<-i+1
ExamAnswers.user[i]<-checkData(mongo.bson.to.list(mongo.cursor.value(cursor))$user)
ExamAnswers.subject[i]<-checkData(mongo.bson.to.list(mongo.cursor.value(cursor))$subject)
ExamAnswers.volume[i]<-checkData(mongo.bson.to.list(mongo.cursor.value(cursor))$volume)
if (length(mongo.bson.to.list(mongo.cursor.value(cursor))$rights)>0) {
ExamAnswers.questionCount[i]<-length(mongo.bson.to.list(mongo.cursor.value(cursor))$rights)
if (class(mongo.bson.to.list(mongo.cursor.value(cursor))$rights)=="list") {
ExamAnswers.rightCount[i]<-sum(unlist(mongo.bson.to.list(mongo.cursor.value(cursor))$rights))
} else {
ExamAnswers.rightCount[i]<-sum(mongo.bson.to.list(mongo.cursor.value(cursor))$rights)
}
}
ExamAnswers.finishDate[i]<-checkData(mongo.bson.to.list(mongo.cursor.value(cursor))$finishDate)
ExamAnswers.type[i]<-checkData(mongo.bson.to.list(mongo.cursor.value(cursor))$type)
}
ExamAnswers.finishDate<-as.formatDate(ExamAnswers.finishDate)
ExamAnswers.finishDate<-as.strDate(ExamAnswers.finishDate)
ExamAnswers102Dat<-data.frame(user=ExamAnswers.user,
year=rep(102,length(ExamAnswers.user)),
subject=ExamAnswers.subject,
volume=ExamAnswers.volume,
questionCount=ExamAnswers.questionCount,
rightCount=ExamAnswers.rightCount,
finishDate=ExamAnswers.finishDate,
type=ExamAnswers.type,
stringsAsFactors = F)
} else {
ExamAnswers102Dat<-NULL
}
collections_name<-mongo.get.database.collections(mongo,"ehanlin_103_jhs_db")
index_collection<-which(collections_name==paste("ehanlin_103_jhs_db",".ExamAnswers",sep=""))
count<-mongo.count(mongo,collections_name[index_collection],list(user=setUser,enabled=TRUE,finished=TRUE))
if (count>0) {
cursor<- mongo.find(mongo,collections_name[index_collection],list(user=setUser,enabled=TRUE,finished=TRUE),limit=count)
ExamAnswers.user<-rep(NA,count)
ExamAnswers.subject<-rep(NA,count)
ExamAnswers.volume<-rep(NA,count)
ExamAnswers.questionCount<-rep(NA,count)
ExamAnswers.rightCount<-rep(NA,count)
ExamAnswers.finishDate<-rep(NA,count)
ExamAnswers.type<-rep(NA,count)
i<-0
while (mongo.cursor.next(cursor)) {
i<-i+1
ExamAnswers.user[i]<-checkData(mongo.bson.to.list(mongo.cursor.value(cursor))$user)
ExamAnswers.subject[i]<-checkData(mongo.bson.to.list(mongo.cursor.value(cursor))$subject)
ExamAnswers.volume[i]<-checkData(mongo.bson.to.list(mongo.cursor.value(cursor))$volume)
if (length(mongo.bson.to.list(mongo.cursor.value(cursor))$rights)>0) {
ExamAnswers.questionCount[i]<-length(mongo.bson.to.list(mongo.cursor.value(cursor))$rights)
if (class(mongo.bson.to.list(mongo.cursor.value(cursor))$rights)=="list") {
ExamAnswers.rightCount[i]<-sum(unlist(mongo.bson.to.list(mongo.cursor.value(cursor))$rights))
} else {
ExamAnswers.rightCount[i]<-sum(mongo.bson.to.list(mongo.cursor.value(cursor))$rights)
}
}
ExamAnswers.finishDate[i]<-checkData(mongo.bson.to.list(mongo.cursor.value(cursor))$finishDate)
ExamAnswers.type[i]<-checkData(mongo.bson.to.list(mongo.cursor.value(cursor))$type)
}
ExamAnswers.finishDate<-as.formatDate(ExamAnswers.finishDate)
ExamAnswers.finishDate<-as.strDate(ExamAnswers.finishDate)
ExamAnswers103Dat<-data.frame(user=ExamAnswers.user,
year=rep(103,length(ExamAnswers.user)),
subject=ExamAnswers.subject,
volume=ExamAnswers.volume,
questionCount=ExamAnswers.questionCount,
rightCount=ExamAnswers.rightCount,
finishDate=ExamAnswers.finishDate,
type=ExamAnswers.type,
stringsAsFactors = F)
} else {
ExamAnswers103Dat<-NULL
}
ExamAnswersDat<-rbind(ExamAnswers102Dat,ExamAnswers103Dat)
if (!is.null(ExamAnswersDat)) {
ExamAnswersDat<-ExamAnswersDat[which(!is.na(ExamAnswersDat$finishDate)),]
summariseExamAnswersDat<-
summarise(group_by(ExamAnswersDat,year,subject,volume),
'作卷數'=length(user),
'作題數'=sum(questionCount,na.rm=T),
'答對率'=round(sum(rightCount,na.rm=T)/sum(questionCount,na.rm=T),digits = 3)
)
summariseExamAnswersDat<-summariseExamAnswersDat[which(summariseExamAnswersDat$'作題數'>0),]
summariseExamAnswersDat<-summariseExamAnswersDat[order(summariseExamAnswersDat$year,
summariseExamAnswersDat$subject,
summariseExamAnswersDat$volume),]
names(summariseExamAnswersDat)[which(names(summariseExamAnswersDat)=="year")]<-"學年度"
names(summariseExamAnswersDat)[which(names(summariseExamAnswersDat)=="subject")]<-"科目"
names(summariseExamAnswersDat)[which(names(summariseExamAnswersDat)=="volume")]<-"冊次"
} else {
summariseExamAnswersDat<-NULL
}
} else {
summariseExamAnswersDat<-NULL
}
return(summariseExamAnswersDat)
})
getUserVideoDat <- reactive({
setUser<-getInputUserId()
if (!is.na(setUser)) {
mongo <-mongo.create(host=sethost)
collections_name<-mongo.get.database.collections(mongo,"ehanlin_102_jhs_db")
index_collection<-which(collections_name==paste("ehanlin_102_jhs_db",".Histories",sep=""))
count<-mongo.count(mongo,collections_name[index_collection],list(user=setUser,action="watchVideo"))
if (count>0) {
cursor<- mongo.find(mongo,collections_name[index_collection],list(user=setUser,action="watchVideo"),limit = count)
Histories.user<-rep(NA,count)
Histories.createDate<-rep(NA,count)
Histories.action<-rep(NA,count)
Histories.info.type<-rep(NA,count)
Histories.subject<-rep(NA,count)
Histories.volume<-rep(NA,count)
i<-0
while (mongo.cursor.next(cursor)) {
i<-i+1
Histories.user[i]<-checkData(mongo.bson.to.list(mongo.cursor.value(cursor))$user)
Histories.createDate[i]<-checkData(mongo.bson.to.list(mongo.cursor.value(cursor))$createDate)
Histories.action[i]<-checkData(mongo.bson.to.list(mongo.cursor.value(cursor))$action)
if (length(mongo.bson.to.list(mongo.cursor.value(cursor))$info)>0) {
if (length(which(names(mongo.bson.to.list(mongo.cursor.value(cursor))$info)=="type"))>0) {
Histories.info.type[i]<-checkData(mongo.bson.to.list(mongo.cursor.value(cursor))$info$type)
}
} else {
next
}
Histories.subject[i]<-checkData(mongo.bson.to.list(mongo.cursor.value(cursor))$subject)
Histories.volume[i]<-checkData(mongo.bson.to.list(mongo.cursor.value(cursor))$volume)
}
Histories.createDate<-as.formatDate(Histories.createDate)
Histories.createDate<-as.strDate(Histories.createDate)
Histories102Dat<-data.frame(year=rep(102,length(Histories.user)),
createDate=Histories.createDate,
action=Histories.action,
info.type=Histories.info.type,
subject=Histories.subject,
volume=Histories.volume,
stringsAsFactors = F)
} else {
Histories102Dat<-NULL
}
collections_name<-mongo.get.database.collections(mongo,"ehanlin_103_jhs_db")
index_collection<-which(collections_name==paste("ehanlin_103_jhs_db",".Histories",sep=""))
count<-mongo.count(mongo,collections_name[index_collection],list(user=setUser,action="watchVideo"))
if (count>0) {
cursor<- mongo.find(mongo,collections_name[index_collection],list(user=setUser,action="watchVideo"),limit=count)
Histories.user<-rep(NA,count)
Histories.createDate<-rep(NA,count)
Histories.action<-rep(NA,count)
Histories.info.type<-rep(NA,count)
Histories.subject<-rep(NA,count)
Histories.volume<-rep(NA,count)
i<-0
while (mongo.cursor.next(cursor)) {
i<-i+1
Histories.user[i]<-checkData(mongo.bson.to.list(mongo.cursor.value(cursor))$user)
Histories.createDate[i]<-checkData(mongo.bson.to.list(mongo.cursor.value(cursor))$createDate)
Histories.action[i]<-checkData(mongo.bson.to.list(mongo.cursor.value(cursor))$action)
if (length(mongo.bson.to.list(mongo.cursor.value(cursor))$info)>0) {
if (length(which(names(mongo.bson.to.list(mongo.cursor.value(cursor))$info)=="type"))>0) {
Histories.info.type[i]<-checkData(mongo.bson.to.list(mongo.cursor.value(cursor))$info$type)
}
} else {
next
}
Histories.subject[i]<-checkData(mongo.bson.to.list(mongo.cursor.value(cursor))$subject)
Histories.volume[i]<-checkData(mongo.bson.to.list(mongo.cursor.value(cursor))$volume)
}
Histories.createDate<-as.formatDate(Histories.createDate)
Histories.createDate<-as.strDate(Histories.createDate)
Histories103Dat<-data.frame(year=rep(103,length(Histories.user)),
createDate=Histories.createDate,
action=Histories.action,
info.type=Histories.info.type,
subject=Histories.subject,
volume=Histories.volume,
stringsAsFactors = F)
} else {
Histories103Dat<-NULL
}
HistoriesDat<-rbind(Histories102Dat,Histories103Dat)
if (!is.null(HistoriesDat)) {
summariseHistoriesDat<-
summarise(group_by(HistoriesDat,year,subject,volume),
'看影片'=length(which(info.type=="video")),
'看進度式影片'=length(which(info.type=="progressVideo")),
'看實驗影片'=length(which(info.type=="labVideo")),
'看動畫'=length(which(info.type=="animation"))
)
names(summariseHistoriesDat)[which(names(summariseHistoriesDat)=="year")]<-"學年度"
names(summariseHistoriesDat)[which(names(summariseHistoriesDat)=="subject")]<-"科目"
names(summariseHistoriesDat)[which(names(summariseHistoriesDat)=="volume")]<-"冊次"
} else {
summariseHistoriesDat<-NULL
}
} else {
summariseHistoriesDat<-NULL
}
return(summariseHistoriesDat)
})
getExamAndVideoDat <- reactive({
summariseExamAnswersDat<-getUserExamDat()
summariseHistoriesDat<-getUserVideoDat()
if (!is.null(summariseExamAnswersDat) && !is.null(summariseHistoriesDat)) {
userExamAndVideoDat<-merge(summariseExamAnswersDat,summariseHistoriesDat,
c("學年度","科目","冊次"),
all = T)
} else if (!is.null(summariseExamAnswersDat) && is.null(summariseHistoriesDat)) {
userExamAndVideoDat<-summariseExamAnswersDat
} else if (is.null(summariseExamAnswersDat) && !is.null(summariseHistoriesDat)) {
userExamAndVideoDat<-summariseHistoriesDat
} else {
userExamAndVideoDat<-NULL
}
return(userExamAndVideoDat)
})
output$userText <- renderText({
if (nchar(input$inputUserMailID)>0) {
mongo <-connection_platform()
collections_name<-mongo.get.database.collections(mongo,setdb_platform)
index_collection<-which(collections_name==paste(setdb_platform,".Users",sep=""))
if (input$userchoice=="email") {
query<-list('email'=input$inputUserMailID)
} else if (input$userchoice=="ID") {
query<-list('_id'=mongo.oid.from.string(input$inputUserMailID))
}
count<-mongo.count(mongo,collections_name[index_collection],query)
if (count>0) {
cursor<- mongo.find(mongo,collections_name[index_collection],query)
while (mongo.cursor.next(cursor)) {
Users.id<-mongo.oid.to.string(mongo.bson.to.list(mongo.cursor.value(cursor))$'_id')
Users.name<-checkData(mongo.bson.to.list(mongo.cursor.value(cursor))$name)
Users.email<-checkData(mongo.bson.to.list(mongo.cursor.value(cursor))$email)
}
Users.name<-paste0("姓名 : ",Users.name," | email : ",Users.email," | userID : ",Users.id)
} else {
Users.name<-"沒有此位使用者"
}
} else {
Users.name<-"沒有此位使用者"
}
return(Users.name)
})
output$examAllTable <- renderDataTable({
getUserExamAllDat()
})
output$examTable <- renderDataTable({
getExamAndVideoDat()
})
output$examAndVideoPlot <- renderGvis({
userExamAndVideoDat <- getExamAndVideoDat()
if (!is.null(userExamAndVideoDat)) {
if ( any(names(userExamAndVideoDat) == "作卷數")==F ) {
userExamAndVideoDat$'作卷數'<-rep(NA,dim(userExamAndVideoDat)[1])
} else if ( any(names(userExamAndVideoDat) == "看影片")==F ) {
userExamAndVideoDat$'看影片'<-rep(NA,dim(userExamAndVideoDat)[1])
userExamAndVideoDat$'看進度式影片'<-rep(NA,dim(userExamAndVideoDat)[1])
userExamAndVideoDat$'看實驗影片'<-rep(NA,dim(userExamAndVideoDat)[1])
userExamAndVideoDat$'看動畫'<-rep(NA,dim(userExamAndVideoDat)[1])
}
df <- summarise(group_by(userExamAndVideoDat,科目),
'作卷數'=sum(作卷數,na.rm = T),
'看影片數'=sum(看影片,na.rm = T)+sum(看進度式影片,na.rm = T)+sum(看實驗影片,na.rm = T)+sum(看動畫,na.rm = T)
)
gvisColumnChart(df)
}
})
})
shinyUI(
navbarPage("eHanlin",
tabPanel("全體作答統計",
sidebarLayout(
sidebarPanel(
dateRangeInput("examDates", label = h3("Date range"),start=Sys.Date()-29,end=Sys.Date(),min="2012-01-01"),
radioButtons("year", "select year:",
choices = c("103","103(高)","102","102(高)"),
),
submitButton('search'),
width=3
),
mainPanel(
dataTableOutput('examAllTable')
)
)
),
tabPanel("使用者測驗統計",
sidebarLayout(
sidebarPanel(
textInput("inputUserMailID", "Input user email or ID", ""),
radioButtons("userchoice", "select email or ID",
choices = c("email","ID"),
inline=T),
submitButton('search'),
width=3
),
mainPanel(
verbatimTextOutput('userText'),
htmlOutput("examAndVideoPlot"),
dataTableOutput('examTable')
)
)
)
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment