-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.R
executable file
·558 lines (482 loc) · 18.1 KB
/
app.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
# BOOTSTRAP CHEAT SHEET FOR SHINY ----
# DS4B 202-R ----
# LIBRARIES ----
library(shiny)
library(tidyverse)
library(lubridate)
library(plotly)
library(shinythemes)
library(shinyjs)
library(DT)
# USER INTERFACE ----
ui <- fluidPage(
title = "Bootstrap Cheat Sheet for Shiny",
div(
class = "container",
id = "page",
# HEADER ----
h1(class = "page-header", "Bootstrap Cheat Sheet", tags$small("For Shiny")),
p(
class = "lead",
"This cheat sheet is the firts part of the ",
a(href = "https://university.business-science.io/", target = "_blank", "Expert Shiny Application Development Course"),
"by Business Science"
),
h2("Table of Contents"),
tags$ul(
tags$li("1.0 Bootstrap Grid System" %>% a(href = "#section_1")),
tags$li("2.0 Working With Text" %>% a(href = "#section_2")),
tags$li("3.0 Text Alignment" %>% a(href = "#section_3")),
tags$li("4.0 Lists" %>% a(href = "#section_4")),
tags$li("5.0 Contextual Colors & Backgrounds" %>% a(href = "#section_5")),
tags$li("6.0 Buttons" %>% a(href = "#section_6")),
tags$li("7.0 Images" %>% a(href = "#section_7")),
tags$li("8.0 Thumbnails" %>% a(href = "#section_8")),
tags$li("9.0 Navbars" %>% a(href = "#section_9")),
tags$li("10.0 Navs" %>% a(href = "#section_10")),
tags$li("11.0 Sidebar Layout" %>% a(href = "#section_11")),
tags$li("12.0 Jumbotron" %>% a(href = "#section_12")),
tags$li("13.0 Panels" %>% a(href = "#section_13")),
tags$li("14.0 Mobile" %>% a(href = "#section_14")),
tags$li("15.0 CSS & Themes" %>% a(href = "#section_15")),
tags$li("16.0 JavaScript (ShinyJS)" %>% a(href = "#section_16"))
),
# 1.0 BOOTSTRAP GRID SYSTEM ----
div(id = "section_1"),
h2("1.0 Bootstrap Grid System"),
div(
class = "container text-center",
fluidRow(
column(
width = 4,
class = "bg-primary",
p("Grid Width 4")
),
column(
width = 4,
class = "bg-warning",
p("Grid Width 4")
),
column(
width = 4,
class = "bg-danger",
p("Grid Width 4")
)
),
fluidRow(
column(
width = 3,
class = "bg-primary",
p("Grid Width 3")
),
column(
width = 9,
class = "bg-info",
p("Grid Width 9")
)
)
),
hr(),
# 2.0 WORKING WITH TEXT ----
div(id = "section_2"),
h2("2.0 Working with Text"),
p(class = "lead", "Business Science University Helps Us Learn Shiny"),
fluidRow(
column(
width = 6,
p("We are creaitng a Bootstrap for Shiny cheat sheet"),
p(strong("In section 1"), "we learned about the", strong(em("Bootstrap grid system."))),
p(tags$mark("In section 2"), ", we learned about working with text in", code("Bootstrap"),".")
),
column(
width = 6,
tags$blockquote(
class = "blockquote-reverse",
p("When learning data science, consistency is more important than quantity."),
tags$footer("Quote by", tags$cite(title = "Matt Dancho", "Matt Dancho"))
)
)
),
hr(),
# 3.0 TEXT ALIGNMENT ----
div(id = "section_3"),
h2("3.0 Text Alignment"),
div(
class = "container",
id = "text_alignment_1",
p(class = "text-left text-lowercase", "Left-aligned Lowercase Text"),
p(class = "text-center text-uppercase", "Center-aligend Uppercase Text"),
p(class = "text-right text-capitalized", "Right-aligned Capitalized Text")
),
div(
class = "container",
id = "text_alignment_2",
fluidRow(
p(class = "text-left text-lowercase", "Left-aligned Lowercase Text") %>% column(width = 4, class = "bg-primary"),
p(class = "text-center text-uppercase", "Center-aligend Uppercase Text") %>% column(width = 4, class = "bg-success"),
p(class = "text-right text-capitalized", "Right-aligned Capitalized Text") %>% column(width = 4, class = "bg-info")
)
),
hr(),
# 4.0 LISTS ----
div(id = "section_4"),
h2("4.0 Lists"),
tags$ol(
tags$li("Item 1"),
tags$li("Item 2"),
tags$li("Item 3"),
tags$li("Item 4")
),
tags$ul(
tags$li("Item 1"),
tags$li("Item 2"),
tags$li("Item 3"),
tags$li("Item 4")
),
tags$ul(
class = "list-inline",
tags$li("Item 1"),
tags$li("Item 2"),
tags$li("Item 3"),
tags$li("Item 4")
),
hr(),
# 5.0 CONTEXTUAL COLORS & BACKGROUNDS ----
div(id = "section_5"),
h2("5.0 Contextual Colors & Backgrounds"),
p(class = "text-primary", "Hello R"),
p(class = "text-success", "Hello R"),
p(class = "text-info", "Hello R"),
p(class = "text-warning", "Hello R"),
p(class = "text-danger", "Hello R"),
p(class = "bg-primary", "Hello R"),
p(class = "bg-success", "Hello R"),
p(class = "bg-info", "Hello R"),
p(class = "bg-warning", "Hello R"),
p(class = "bg-danger", "Hello R"),
hr(),
# 6.0 BUTTONS ----
div(id = "section_6"),
h2("6.0 Buttons"),
h3("Contextual Buttons"),
div(
class = "container",
a(href = "https://www.business-science.io/", class = "btn btn-default", "Go to Business Science"),
a(href = "https://www.business-science.io/", class = "btn btn-primary", "Go to Business Science"),
a(href = "https://www.business-science.io/", class = "btn btn-success", "Go to Business Science"),
a(href = "https://www.business-science.io/", class = "btn btn-warning", "Go to Business Science"),
a(href = "https://www.business-science.io/", class = "btn btn-danger", "Go to Business Science"),
a(href = "https://www.business-science.io/", class = "btn btn-info", "Go to Business Science")
),
br(),
h3("Sizing Buttons"),
div(
class = "container",
a(href = "https://www.business-science.io/", class = "btn btn-default btn-lg", "Go to Business Science"),
a(href = "https://www.business-science.io/", class = "btn btn-primary btn-md", "Go to Business Science"),
a(href = "https://www.business-science.io/", class = "btn btn-success btn-sm", "Go to Business Science"),
a(href = "https://www.business-science.io/", class = "btn btn-warning btn-xs", "Go to Business Science")
),
br(),
h3("Shiny Buttons"),
div(
class = "container",
actionButton(
inputId = "btn_1",
label = "Shiny Button - Click Me!",
class = "btn-primary btn-lg",
icon = icon("sync", class = "fa-1x", lib = "font-awesome")),
textOutput(
outputId = "btn_1_txt"
)
),
hr(),
# 7.0 IMAGES ----
div(id = "section_7"),
h2("7.0 Images"),
div(
class = "container",
column(
width = 4,
img(class = "thumbnail img-responsive", src = "business-science-logo.png", style = "width:200px;")
),
column(
width = 4,
img(class = "img-rounded img-responsive", src = "matt-pic.jpg", style = "width:200px;")
),
column(
width = 4,
img(class = "img-circle img-responsive", src = "matt-pic.jpg", style = "width:200px;")
),
),
hr(),
# 8.0 THUMBNAILS ----
div(id = "section_8"),
h2("8.0 Thumbnails"),
fluidRow(
column(
width = 4,
div(
class = "thumbnail text-center",
#style = "padding: 20px;",
img(class = "img-rounded img-responsive", src = "matt-pic.jpg"),
h3("Thumbnail Label"),
p("Text about this thumbnail"),
a(class = "btn btn-primary btn-sm", href = "#", "Learn More")
)
),
column(
width = 4,
div(
class = "thumbnail text-center",
#style = "padding: 20px;",
img(class = "img-rounded img-responsive", src = "matt-pic.jpg"),
h3("Thumbnail Label"),
p("Text about this thumbnail"),
a(class = "btn btn-primary btn-sm", href = "#", "Learn More")
)
),
column(
width = 4,
div(
class = "thumbnail text-center",
#style = "padding: 20px;",
img(class = "img-rounded img-responsive", src = "matt-pic.jpg"),
h3("Thumbnail Label"),
p("Text about this thumbnail"),
a(class = "btn btn-primary btn-sm", href = "#", "Learn More")
)
)
),
hr(),
# 9.0 NAVBARS ----
div(id = "section_9"),
h2("9.0 Navbars"),
navbarPage(
title = "Business Science",
inverse = TRUE,
collapsible = TRUE,
tabPanel(
title = "What is Shiny?",
value = "page_1",
h1(class = "page-header", "This is Shiny", tags$small("A framework for buiding web apps with R")),
p("All the cool features of Shiny"),
),
tabPanel(
title = "What is Bootstrap?",
value = "page_2",
h1("What is Bootstrap?", tags$small("A popular web framework that extends HTML and CSS")),
p("All of the cool features of the Bootstrap")
),
navbarMenu(
title = "Using Shiny and Bootstrap",
tabPanel(title = "Make Plots"),
tabPanel(title = "Add Shiny Components"),
"----",
tabPanel(title = "More Info")
)
),
hr(),
# 10.0 NAVS ----
div(id = "section_10"),
h2("10.0 Navs"),
h3("Tabset Panel"),
tabsetPanel(
id = "tabset_1",
type = "tabs",
tabPanel(
title = "Shiny",
h3("What is Shiny?"),
p("Shiny is awesome")
),
tabPanel(
title = "Bootstrap",
h3("What is Bootstrap?"),
p("Bootstrap is awesome")
)
),
br(),
h3("Navlist Panel"),
navlistPanel(
id = "navlist_1",
tabPanel(
title = "Shiny",
h3("What is Shiny?"),
p("Shiny is awesome")
),
tabPanel(
title = "Bootstrap",
h3("What is Bootstrap?"),
p("Bootstrap is awesome")
)
),
hr(),
# 11.0 SIDEBAR LAYOUT ----
div(id = "section_11"),
h2("Sidebar Layout"),
sidebarLayout(
sidebarPanel = sidebarPanel(
width = 4,
p("UI Elements Go Here"),
dateRangeInput(
inputId = "date_range_1",
label = "Enter a Date Range"
)
),
mainPanel = mainPanel("Plot Elements and Analysis Go Here", width = 8)
),
hr(),
# 12.0 JUMBOTRONS ----
div(id = "section_12"),
h2("12.0 Jumbotron"),
div(
class = "jumbotron",
style = "background:url('business-science-logo.png'); background-size:cover;",
div(
class = "container",
style = "background-color:#9999999c;",
h1("Learning Shiny"),
p("It's your solution for building web appplication with ", code("R")),
"Learn More" %>% a(class = "btn bt-lg btn-primary", href = "#") %>% p()
),
),
hr(),
# 13.0 PANLES ----
div(id = "section_13"),
h2("13.0 Panels"),
div(
class = "panel panel-primary",
div(
class = "panel-heading",
h3("Chart Title - Plot of MPG vs. WT")
),
div(
class = "panel-body bg-info",
p("Insert Chart"),
plotlyOutput(outputId = "mtcars")
),
div(
class = "panel-footer",
style = "background-color: #d9edf7;",
p("Footer - Caption: This is a plot of vehicle weight vs. fuel efficiency") %>% tags$small()
)
),
hr(),
# 14.0 MOBILE ----
div(id = "section_14"),
h2("14.0 Mobile"),
fluidRow(
class = "hidden-xs",
div(
class = "jumbotron",
h1("Learning Shiny"),
p(class = "lead", "Will help you distribute interactive data products."),
a(class = "btn btn-primary btn-lg", href = "#", "Learn More")
)
),
fluidRow(
class = "hidden-sm hidden-md hidden-lg",
div(
class = "thumbnail text-center",
img(class = "img-responsive", style = "width:200px;", src = "business-science-logo.png"),
h3("Learning Shiny"),
p(class = "lead", "Will help you distribute interactive data products."),
a(class = "btn btn-primary btn-sm", href = "#", "Learn More")
)
),
hr(),
# 15.0 CSS & THEMES ----
div(id = "section_15"),
h2("15.0 CSS & Themes"),
fluidPage(
theme = shinytheme("flatly"),
# themeSelector()
tags$head(
#tags$link(rel = "stylesheet", type = "text/css", href = "bootstrap.min.css"),
tags$link(rel = "stylesheet", type = "text/css", href = "my_styles.css")
)
),
hr(),
# 16.0 JAVASCRIPT ----
div(id = "section_16"),
h2("16.0 Javascript (ShinyJS)"),
useShinyjs(),
fluidRow(
column(
width = 4,
class = "well",
# Toggle Button
shiny::actionButton(inputId = "toggle_form", label = "Toggle Button"),
# Conrols
div(
id = "controls",
br(),
shiny::textInput(inputId = "first_name", label = "First Name", placeholder = "Enter your first name"),
shiny::textInput(inputId = "email", label = "Email", placeholder = "Email"),
shiny::actionButton(inputId = "submit_form", label = "Submit")
) %>% shinyjs::hidden(),
# Alert - Thank you
div(
id = "thank_you",
br(),
div(
class = "alert alert-success",
role = "alert",
p("Thank you!", shiny::actionButton(inputId = "close_alert", label = "x", class = "pull-right btn-xs"))
)
) %>% shinyjs::hidden()
),
column(
width = 8,
DTOutput(outputId = "new_user_dt")
)
),
div(style = "height: 400px;")
)
)
# SERVER ----
server <- function(input, output, session) {
counter <<- 0
btn_1_click <- eventReactive(input$btn_1, {
TRUE
counter <<- counter + 1
})
output$btn_1_txt <- renderText({
if (btn_1_click()) {
str_glue("You clicked me {counter} times!")
}
})
# Plot mtcars
output$mtcars <- renderPlotly({
g <- mtcars %>%
ggplot(aes(wt, mpg)) +
geom_point()
ggplotly(g)
})
# 16.0 ShinyJS ----
shinyjs::onclick(id = "toggle_form", {
shinyjs::toggle(id = "controls", anim = TRUE)
})
observe({
shinyjs::toggleState(id = "submit_form", condition = {
!(input$first_name == "") && !(input$email == "")
})
})
new_user_tbl <- eventReactive(eventExpr = input$submit_form, {
new_user_tbl <- tibble(
first_name = input$first_name,
email = input$email,
timestamp = lubridate::now()
)
})
output$new_user_dt <- renderDT({
new_user_tbl() %>% datatable()
})
observeEvent(eventExpr = input$submit_form, {
shinyjs::toggle(id = "thank_you", anim = TRUE, animType = "fade", time = 0.5)
})
shinyjs::onclick(id = "close_alert", {
shinyjs::toggle(id = "thank_you", anim = TRUE, animType = "fade", time = 0.5)
})
}
shinyApp(ui = ui, server = server)