-
Notifications
You must be signed in to change notification settings - Fork 0
/
dependencies.R
114 lines (95 loc) · 4.47 KB
/
dependencies.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
#http://shiny.rstudio-staging.com/articles/css.html?fbclid=IwAR05yMZBYOhUYm6ljHQ5Gyvc1hb_DvtRbJCOEusHYq4tTmkRupaT7S_0BAg#:~:text=If%20you%20have%20CSS%20saved,save%20the%20file%20in%20www%20
#https://bootswatch.com/
bs_card <- function(card_div_class="accordion",
card_item_class="accordion-item",
card_button_class="accordion-button",
card_body_class="accordion-body",
header_text, title_text, body_text) {#font_color, bg_color, header_text, title_text, body_text) {
shiny::div(
class=card_div_class, #glue::glue("card text-{font_color} bg-{bg_color} mb-3"),
style="width: 80%;",
shiny::div(
class=card_item_class,
shiny::HTML(
glue(
'<button class={card_button_class} type="button" data-bs-toggle="collapse" data-bs-target="#collapseBody" aria-expanded="true" aria-controls="collapseBody">
{header_text}
</button>'
)
),
shiny::div(class=card_body_class, id="collapseBody",
shiny::h5(
title_text
),
shiny::p(
body_text
)
)
)
)
}
linebreaks <- function(n){HTML(strrep(br(), n))}
##Creating a module that can be reused twice
singleGuessUI <- function(id){
ns <- NS(id)
column(12,
textInput(ns("guess_number"),
label=h1("Guess a number between 1 and 10"#,
# style = "font-family: 'Lobster', cursive;
# font-weight: 500; line-height: 1.1;
# color: #4d3a7d;"
),
placeholder = "e.g. 5",
width='80%'),actionButton(ns("BtnPlay"),"PLAY", width="50%", icon("play"), style='padding: 0%; font-size:300%; color:#228B22; webkit-tap-highlight-color: transparent;'),
linebreaks(2),
uiOutput(ns("result")),
align="center"
)
}
singleGuessServer <- function(id, n=1){
moduleServer(id,
function(input, output, session){
ns <- session$ns
outcome_final <- reactiveVal(0)
counter <- reactiveVal(0)
observeEvent(input$BtnPlay,{
updateTextInput(inputId = "guess_number",value="",placeholder = input$guess_number)
if(!is.na(as.numeric(input$guess_number))){
counter(counter()+1)
gen_number <- sample(1:10,1)
outcome_single <- as.integer(gen_number==as.numeric(input$guess_number))
outcome_current <- outcome_final()+outcome_single
outcome_final(outcome_current)
result <- ifelse(rep(outcome_single,2), c("Won!!!","green"), c("Lost!!!","red"))
if(counter() < n){
score_text <- glue("Your current score is {outcome_final()}/{counter()}")
color_score <- "black"
} else if(counter() == n){
score_text <- glue("Your FINAL score is {outcome_final()}/{n}")
color_score <- "green"
counter(0) #resets the counter to 0
outcome_final(0) #resets the outcome_final reactive value to 0
}
output$result <- renderUI(
bs_card(
header_text = "Here comes your outcome",
title_text = glue("The generated number is: {gen_number}"),
body_text = div(h1(result[1], style=glue('color:{result[2]}')),
h5(score_text, style = glue('color:{color_score}'))
)
)
)
} else{
output$result <- renderUI(
bs_card(
header_text = "Here comes your outcome",
title_text = "Invalid number!",
body_text = div(h1("Please try again with a valid number", style='color:red')
)
)
)
}
}, ignoreInit = TRUE)
}
)
}