-
Notifications
You must be signed in to change notification settings - Fork 239
/
Copy pathfeedback.R
76 lines (73 loc) · 2.53 KB
/
feedback.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
# Provide exercise feedback
feedback <- function(message, correct, type, location) {
feedback_validated(list(
message = message,
correct = correct,
type = type,
location = location
))
}
# return feedback if it's valid (with defaults), otherwise throw an error
feedback_validated <- function(feedback) {
if (!length(feedback)) {
return(feedback)
}
if (!(is.list(feedback) && all(c("message", "correct") %in% names(feedback)))) {
stop("Feedback must be a list with 'message' and 'correct' fields", call. = FALSE)
}
if (!(is.character(feedback$message) || inherits(feedback$message, c("shiny.tag", "shiny.tag.list")))) {
stop("The 'message' field of feedback must be a character vector or an htmltools tag or tagList", call. = FALSE)
}
if (!is.logical(feedback$correct)) {
stop("The 'correct' field of feedback must be a logical (i.e., boolean) value", call. = FALSE)
}
# Fill in type/location defaults and check their value
feedback$type <- feedback$type[1] %||% "auto"
feedback$location <- feedback$location[1] %||% "append"
feedback_types <- c("auto", "success", "info", "warning", "error", "custom")
if (!feedback$type %in% feedback_types) {
stop("Feedback 'type' field must be one of these values: ",
paste(feedback_types, collapse = ", "), call. = FALSE)
}
feedback_locations <- c("append", "prepend", "replace")
if (!feedback$location %in% feedback_locations) {
stop("Feedback 'location' field must be one of these values: ",
paste(feedback_locations, collapse = ", "), call. = FALSE)
}
if (feedback$type %in% "auto") {
feedback$type <- if (feedback$correct) "success" else "error"
}
feedback
}
feedback_as_html <- function(feedback) {
if (!length(feedback)) {
return(feedback)
}
feedback <- feedback_validated(feedback)
if (feedback$type %in% "custom") {
return(div(feedback$message))
}
if (feedback$type %in% "error") {
feedback$type <- "danger"
}
if (feedback$type %in% c("success", "info", "warning", "danger")) {
return(div(
role = "alert",
class = paste0("alert alert-", feedback$type),
feedback$message
))
}
stop("Invalid message type specified.", call. = FALSE)
}
# helper function to create tags for error message
error_message_html <- function(message, style = "code") {
switch(
style,
alert = div(class = "alert alert-danger", role = "alert", message),
code = ,
pre(
code(class = "text-danger", message, .noWS = c("before", "after")),
.noWS = c("before", "after")
)
)
}