forked from r-cas/shinymathexample
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.R
executable file
·119 lines (92 loc) · 3.43 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
library(Ryacas)
library(iomath)
library(shiny)
choices_x_coef <- c("a", "2*a", "3*a")
choices_x_pow <- 1:3
compare_grid <- expand.grid(
x = seq(-10, 10, len = 6),
a = seq(-10, 10, len = 6)
)
# Define server logic required to draw a histogram
shinyServer(function(input, output, session) {
generate_f <- function() {
x_coef <- sample(choices_x_coef, 1)
x_pow <- sample(choices_x_pow, 1)
x_part <- paste0(x_coef, "*x^", x_pow)
eq <- ysym(x_part)
eq
}
problem_f_eq <- generate_f()
true_ans <- list(
x = deriv(problem_f_eq, "x")
)
output$problem <- renderUI({
problem <- paste0("Let $$f(x) = ", tex(problem_f_eq), ".$$",
"Calculate the derivative with respect to \\(x\\) and enter the result below.")
res <- withMathJax(
helpText(problem)
)
return(res)
})
feedback <- reactiveValues(x = FALSE)
feedback_var_numeric <- function(input, var, true_ans) {
reply <- input[[paste0("answer_", var)]]
reply <- gsub(" ", "", reply, fixed = TRUE)
# Empty string
if (nchar(reply) == 0L) {
reply <- "0"
}
parsed_input <- iomath::prepare_input(reply,
replace_comma = TRUE,
insert_products = TRUE)
if (inherits(parsed_input, "error")) {
return(list(correct = FALSE,
feedback = "Could not prepare the input (remember that I'm simple-minded!)."))
}
reply_sym <- tryCatch(Ryacas::ysym(parsed_input),
error = function(e) e)
if (inherits(reply_sym, "error")) {
return(list(correct = FALSE,
feedback = "Could not understand the input (remember that I'm simple-minded!)."))
}
is_correct <- tryCatch(iomath::compare_reply_answer(reply = reply,
answer = true_ans[[var]],
compare_grid = compare_grid),
error = function(e) e)
if (inherits(is_correct, "error")) {
return(list(correct = FALSE,
feedback = paste0("Error: ", is_correct$message)))
}
prefix <- if (is_correct) {
"CORRECT"
} else {
"WRONG"
}
details <- paste0("Expected $$", tex(true_ans[[var]]), "$$ and got $$",
tex(reply_sym), "$$")
feedback <- paste0(prefix, ": ", details)
return(list(correct = is_correct,
feedback = feedback))
}
observeEvent(input$go, {
feedback$x <- feedback_var_numeric(input = input,
var = "x",
true_ans = true_ans)
})
output$feedback <- renderUI({
req(feedback$x)
col <- if (feedback$x$correct) {
"green"
} else {
"red"
}
withMathJax(
h3("Feedback"),
h4("df / dx"),
div(
style = paste0("color: ", col),
feedback$x$feedback
)
)
})
})