-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.R
86 lines (74 loc) · 2.3 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
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
library(shiny)
source("library.R")
options(stringsAsFactors=FALSE)
shinyServer(function(input, output, session) {
# load the dictionary
dict = reactive({
file = if(is.null(input$file)) f else input$file$datapath
read.csv(req(file), header=TRUE, encoding="utf-8")
})
rv = reactiveValues(reset=0)
finito = FALSE
n = reactive(nrow(dict()))
# quiz set-up
observe({
rv$reset
rv$checklist = seq_len(n())
rv$i = 1
rv$hint = ""
rv$fin = FALSE
updateActionButton(session, inputId="submit", label="Done")
})
output$qestion = renderText(rv$question)
output$hint = renderText(rv$hint)
# button action
observeEvent(input$submit, {
if(rv$fin) { # reset quiz when finish reached
rv$reset = rv$reset + 1
} else { # go ahead with quiz
i = rv$i
correctAnswer = dict()[rv$checklist[i], 2]
if(input$answer==correctAnswer) { # correct answer
if(rv$hint=="") # hint was not needed
rv$checklist[i] = NA # remove word from queue
else
rv$hint = "" # reset hint
i = i + 1 # go to another word
while(is.na(rv$checklist[i])) { # skip solved questions
if(i > n()) { # arrived at the end
if(all(is.na(rv$checklist))) { # hurra! quiz solved!
finito = TRUE
rv$fin = TRUE
break
} else { # restart list if still unanswered questions
i = 1
}
} else { # look at next question
i = i + 1
}
}
} else { # incorrect answer, print hint
rv$hint = correctAnswer
}
if(finito) { # actions when quiz solved
rv$question = ""
rv$hint = ""
updateActionButton(session, inputId="submit", label="Again please!")
} else {
rv$i = i
}
updateTextInput(session, "answer", value="") # reset user input
session$sendCustomMessage(type='speakme', correctAnswer)
}})
# activate or deactivate text field
observe({shinyjs::toggleState("answer", !rv$fin)})
# set question
observe({
quest = dict()[rv$checklist[rv$i],1]
rv$question = ifelse(is.na(quest), "Well done!", quest)
})
# dieable/anable language type depending on speak
observe({
shinyjs::toggleState("lang", input$speak==TRUE)
})
})