+ Show code- Hide code
# This is code Op_en7421/dayquestion on page [[Great game of Turkey]]
library(OpasnetUtils)
question <- function() {
ques <- opbase.data("Op_en7421.questions")
colnames(ques)[colnames(ques) == "Result"] <- "Option"
qvote <- opbase.data("Op_en7421.question_vote")
qvote <- unique(qvote[qvote$Date == as.character(Sys.Date()) , ]) # Remove double votes
qvote <- aggregate(qvote["User"], by = qvote["Result"], FUN = length)
qvote <- qvote[order(qvote$User, descending = TRUE) , ][1,] # Choose the row with most votes
temp <- merge(ques, qvote, by.x = "Obs", by.y = "Result")["Question"]
# Find the most popular options
ovote <- opbase.data("Op_en7421.option_vote")
ovote <- ovote[nrow(ovote):1 , ] #Turn newest first
#ovote <- ovote[!duplicated(ovote[c("Date", "User") , ]) , ] # Only keep the last vote
out <- merge(merge(temp, ques), ovote, by.x = "Obs", by.y = "Result")
out <- aggregate(out["User"], by = out["Option"], FUN = length)
out <- out[order(out$User, decreasing = TRUE) , ][1:4,] # Choose the 4 rows with most votes
out <- out[!is.na(out$User) , "Option", drop = FALSE]
return(merge(temp, out))
}
mergeusers <- function(happ) {
if(length(happ$User) != length(happ$User[happ$User == wiki_username])) {
test1 <- happ[happ$User == wiki_username , ]
test1$User <- "You"
test2 <- happ[happ$User != wiki_username , ]
test2$User <- "Others"
happ <- combine(test1, test2)
}
return(happ)
}
happiness <- Ovariable("happiness", ddata = "Op_en7421.happiness", getddata = FALSE)
progress <- Ovariable("progress", ddata = "Op_en7421.progress", getddata = FALSE)
questions <- Ovariable("questions", ddata = "Op_en7421.questions", getddata = FALSE)
choices <- Ovariable("choices",
dependencies = data.frame(Name = "happiness"),
formula = function(...) {
dat <- opbase.data("Op_en7421.choices")
colnames(dat)[colnames(dat) == "Result"] <- "Option"
dat$Obs <- NULL
dat$Result <- 1
return(dat)
}
)
questions <- Ovariable("questions",
dependencies = data.frame(Name = "happiness"),
formula = function(...) {
dat <- opbase.data("Op_en7421.questions")
colnames(dat)[colnames(dat) == "Result"] <- "Option"
dat$Obs <- NULL
dat$Result <- 1
return(dat)
}
)
happindex <- Ovariable("happindex",
dependencies = data.frame(Name = c(
"happiness",
"choices",
"questions"
)),
formula = function(...) {
happiness <- oapply(happiness, cols = "User", FUN = mean)
happindex <- choices * happiness * questions
return(happindex)
}
)
progindex <- Ovariable("progindex",
dependencies = data.frame(Name = c(
"progress",
"choices",
"questions"
)),
formula = function(...) {
progress <- oapply(progress, cols = "User", FUN = mean)
progindex <- choices * progress * questions
return(progindex)
}
)
objects.store(list = ls())
| |