-
Notifications
You must be signed in to change notification settings - Fork 0
/
Event-Chain.R
105 lines (78 loc) · 2.66 KB
/
Event-Chain.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
# Skeleton for an event chain in Shiny
# For the server function, check here:
# https://www.statworx.com/en/content-hub/blog/master-r-shiny-one-trick-to-build-maintainable-and-scaleable-event-chains/
library(shiny)
ui <- fluidPage(
titlePanel("A Simple Event Chain in Shiny"),
h2("Choose wisely!"),
fluidRow(
column(4, offset = 1,
h3("Why were you born?"),
h4("Choose at least one important reason"),
checkboxGroupInput(
"whyBorn", label = "",
choiceNames = c("To get rich", "To climb the career ladder as far as possible",
"To discover the immortal aspect inside", "To help others"),
choiceValues = c(1, 2, 11, 12)
)
),
column(4, offset = 1,
h3("Which superpower do you desire the most?"),
radioButtons(
"superpower", label = "",
choiceNames = c("Successful Businessman", "Master Coder",
"Intuition for other people's needs", "Unwavering Faith"),
choiceValues = c(1, 2, 11, 12)
)
)
),
fluidRow(
column(2, offset = 5,
actionButton("evaluate", "Evaluate my choices!",
class = "btn-primary btn-lg",
icon = icon("yin-yang"), verify_fa = FALSE)
)
)
)
server <- function(input, output, session) {
# First evaluate superpower and provide feedback,
# then evaluate why born and provide feedback (or open specific tabs)
rv <- reactiveValues(
superpower = FALSE,
whyBorn = FALSE
)
observe({
rv$superpower <- !(rv$superpower)
}) |> bindEvent(input$evaluate, ignoreInit = TRUE)
observe({
num <- as.numeric(input$superpower)
showModal(modalDialog(
title = "Evaluation: Superpower",
if (num > 10) {
"You're in on the Spiritual Path!"
} else {
"Best of luck!"
},
footer = actionButton("stepWhyBorn", "Next: Evaluate 'Why born?'")
))
}) |> bindEvent(rv$superpower, ignoreInit = TRUE)
observe({
rv$whyBorn <- !(rv$whyBorn)
}) |> bindEvent(input$stepWhyBorn, ignoreInit = TRUE)
observe({
num <- sum(as.numeric(input$whyBorn))
showModal(modalDialog(
title = "Evaluation: Why were you born?",
if (num > 10) {
"You're in on the Spiritual Path!"
} else {
"Best of luck!"
},
footer = actionButton("end", "OK - end of App")
))
}) |> bindEvent(rv$whyBorn, ignoreInit = TRUE)
observe({
stopApp(42)
}) |> bindEvent(input$end, ignoreInit = TRUE)
}
shinyApp(ui, server)