-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathui.qmd
1594 lines (1222 loc) · 40.6 KB
/
ui.qmd
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
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
---
always_allow_html: true
---
# Customizing UI
Understanding how to build a user interface
```{r setup, include=FALSE}
library(shiny)
library(tidyverse)
library(rsconnect)
knitr::opts_chunk$set(
echo = FALSE,
fig.align = "center",
out.width = "100%"
)
```
## 4.1 Interface builder functions
###
Earlier in the course we mentioned that the UI is ultimately built with HTML. We actually use R functions to make the UI, but they get translated to HTML.
In this module we build on this idea to develop more complex app interfaces. More specifically, we discuss **tags**.
### tags
Shiny comes with a list of functions saved under tags that allow us to access HTML tags and use them to add static (as opposed to reactive) content to our apps.
The `tags` object in shiny is a list of 100+ simple functions for constructing HTML documents. Each of the elements in this list is a function that maps to an HTML tag.
```{r, echo = TRUE, eval = TRUE}
names(tags)
```
### tag -> HTML
For example, let's use the `b` tag, which is used for bolding text in HTML. The function is `tags$b()`.
You can pass a text string, in quotation marks, to this function, like "This is my first app".
```{r, echo= TRUE, eval = FALSE}
tags$b("This is my first app")
```
Then R translates the text string to HTML:
```
<b>This is my first app</b>
```
The HTML is then rendered as bolded text when the user sees it:
```{r, echo=FALSE}
tags$b("This is my first app")
```
### Header tags
So how would you use these tags in building an app?
You might use the various levels of headers if it makes sense for to have subheadings in my app.
For example `tags$h1()`, `tags$h2()`, and `tags$h3()` to create first, second, and third level headings, etc.
```{r, echo = TRUE, eval = FALSE}
library(shiny)
# Define UI with tags
ui <- fluidPage(
tags$h1("First level heading"),
tags$h2("Second level heading"),
tags$h3("Third level heading")
)
# Define server function that does nothing :)
server <- function(input, output, session) {}
# Create the app object
shinyApp(ui = ui, server = server)
```
---
```{r, echo=FALSE}
fluidPage(
tags$h1("First level heading"),
tags$h2("Second level heading"),
tags$h3("Third level heading")
)
```
---
### Linked text
If you have references you want to link to at the bottom of you app, you can use the a tag with the `href` argument for specifying a URL.
```{r, echo = TRUE, eval = FALSE}
library(shiny)
# Define UI with tags
ui <- fluidPage(
tags$h1("Awesome title"),
tags$br(), # line break
tags$a("This app is built with Shiny.", href = "http://shiny.rstudio.com/")
)
# Define server fn that does nothing :)
server <- function(input, output, session) {}
# Create the app object
shinyApp(ui = ui, server = server)
```
---
```{r, echo=FALSE}
fluidPage(
tags$h1("Awesome title"),
tags$br(), # line break
tags$a("This app is built with Shiny.", href = "http://shiny.rstudio.com/")
)
```
---
### Nested tags
You can also nest tags within each other to create something like a new paragraph with the `p` tag and some text in that paragraph where certain words are italicized with the `em` tag and certain are bolded with the `b` tag.
```{r, echo = TRUE, eval = FALSE}
library(shiny)
# Define UI with tags
ui <- fluidPage(
tags$p(
"Lorem ipsum",
tags$em("dolor"),
"sit amet,",
tags$b("consectetur"),
"adipiscing elit."
)
)
# Define server fn that does nothing :)
server <- function(input, output, session) {}
# Create the app object
shinyApp(ui = ui, server = server)
```
---
```{r, echo=FALSE}
fluidPage(
tags$p(
"Lorem ipsum",
tags$em("dolor"),
"sit amet,",
tags$b("consectetur"),
"adipiscing elit."
)
)
```
---
### Common tags
Additionally, the most commonly used tags are wrapped in their own functions and you can use them without the tags list.
```{r, eval = FALSE, echo = TRUE}
tags$p(...) -> p(...)
tags$h1(...) -> h1(...)
tags$h2(...) -> h2(...)
tags$h3(...) -> h3(...)
tags$h4(...) -> h4(...)
tags$h5(...) -> h5(...)
tags$h6(...) -> h6(...)
tags$a(...) -> a(...)
tags$br(...) -> br(...)
tags$div(...) -> div(...)
tags$span(...) -> span(...)
tags$pre(...) -> pre(...)
tags$code(...) -> code(...)
tags$img(...) -> img(...)
tags$strong(...) -> strong(...)
tags$em(...) -> em(...)
tags$hr(...) -> hr(...)
```
###
These are functions like:
* `a()` for anchor text.
```{r, echo = TRUE, eval = FALSE}
tags$a("Anchor text")
```
```
<a>Anchor text</a>
```
```{r, echo = TRUE, eval = FALSE}
a("Anchor text")
```
```
<a>Anchor text</a>
```
* `br()` for a line break.
```{r, echo = TRUE, eval = FALSE}
tags$br()
```
```
<br>
```
```{r, echo = TRUE, eval = FALSE}
br()
```
```
<br>
```
* `code()` for displaying code in monospace form
```{r, echo = TRUE, eval = FALSE}
tags$code("Monospace text")
```
```
<code>Monospace text</code>
```
```{r, echo = TRUE, eval = FALSE}
code("Monospace text")
```
```
<code>Monospace text</code>
```
* And the heading functions we mentioned earlier
```{r, echo = TRUE, eval = FALSE}
tags$h1("First level header")
```
```
<h1>First level header</h1>
```
```{r, echo = TRUE, eval = FALSE}
h1("First level header")
```
```
<h1>First level header</h1>
```
The function names correspond to the tag names, and the functions accept text strings as arguments. For example `tags$h1("First level heading")` is equivalent to `h1("First level heading")`.
### HTML
If you're comfortable with HTML, an alternative is to directly use HTML syntax and wrap your HTML code with the HTML function.
```{r, eval = FALSE, echo = TRUE}
HTML("Hello world, <br/> and then a line break.")
```
```
Hello world, <br/> and then a line break.
```
### Practice
Next, let's work on some exercises on adding HTML elements to our apps to customize appearance.
These next exercises will demo a few of the simpler interface builder options with HTML code.
### Practice - Add text with HTML tags
#### Your turn
Add explanatory text to your movie app by using HTML tags in the main panel.
Below is some sample text you can add. Note that you should replace `[NUMBER OF MOVIES]` with with the number of movies in the sample, and instead of hardcoding this value,
you can use `nrow(movies)` to calculate it.
```
These data were obtained from <a href="http://www.imbd.com/">IMBD</a> and
<a href="https://www.rottentomatoes.com/">Rotten Tomatoes</a>.
The data represent _[NUMBER OF MOVIES]_ randomly sampled movies released between 1972 to 2014 in the United States.
```
:::proj
*Navigate to the RStudio Cloud Project titled __4-1a Add text with HTML tags__ in your RStudio Workspace to see this code in action*
[<i class="fa fa-cloud"></i> Go to RStudio Cloud Project](https://rstudio.cloud/spaces/81721/join?access_code=I4VJaNsKfTqR3Td9hLP7E1nz8%2FtMg6Xbw9Bgqumv){.btn .test-drive}
:::
```{r ex-4-1a-html, echo = TRUE, eval = FALSE}
# Load packages ----------------------------------------------------------------
library(shiny)
library(ggplot2)
library(tools)
# Load data --------------------------------------------------------------------
load("movies.RData")
# Define UI --------------------------------------------------------------------
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
selectInput(
inputId = "y",
label = "Y-axis:",
choices = c(
"IMDB rating" = "imdb_rating",
"IMDB number of votes" = "imdb_num_votes",
"Critics Score" = "critics_score",
"Audience Score" = "audience_score",
"Runtime" = "runtime"
),
selected = "audience_score"
),
selectInput(
inputId = "x",
label = "X-axis:",
choices = c(
"IMDB rating" = "imdb_rating",
"IMDB number of votes" = "imdb_num_votes",
"Critics Score" = "critics_score",
"Audience Score" = "audience_score",
"Runtime" = "runtime"
),
selected = "critics_score"
),
selectInput(
inputId = "z",
label = "Color by:",
choices = c(
"Title Type" = "title_type",
"Genre" = "genre",
"MPAA Rating" = "mpaa_rating",
"Critics Rating" = "critics_rating",
"Audience Rating" = "audience_rating"
),
selected = "mpaa_rating"
),
sliderInput(
inputId = "alpha",
label = "Alpha:",
min = 0, max = 1,
value = 0.5
),
sliderInput(
inputId = "size",
label = "Size:",
min = 0, max = 5,
value = 2
),
textInput(
inputId = "plot_title",
label = "Plot title",
placeholder = "Enter text to be used as plot title"
),
actionButton(
inputId = "update_plot_title",
label = "Update plot title"
)
),
mainPanel(
tags$br(),
___, # add text here
plotOutput(outputId = "scatterplot")
)
)
)
# Define server ----------------------------------------------------------------
server <- function(input, output, session) {
new_plot_title <- eventReactive(
eventExpr = input$update_plot_title,
valueExpr = {
toTitleCase(input$plot_title)
}
)
output$scatterplot <- renderPlot({
ggplot(data = movies, aes_string(x = input$x, y = input$y, color = input$z)) +
geom_point(alpha = input$alpha, size = input$size) +
labs(title = new_plot_title())
})
}
# Create the Shiny app object --------------------------------------------------
shinyApp(ui = ui, server = server)
```
<details>
<summary>Show solution</summary>
See the following code chunk for the solution to the exercise above.
```{r ex-4-1a-html-solution, echo = TRUE, eval = FALSE}
# Load packages ----------------------------------------------------------------
library(shiny)
library(ggplot2)
library(tools)
# Load data --------------------------------------------------------------------
load("movies.RData")
# Define UI --------------------------------------------------------------------
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
selectInput(
inputId = "y",
label = "Y-axis:",
choices = c(
"IMDB rating" = "imdb_rating",
"IMDB number of votes" = "imdb_num_votes",
"Critics Score" = "critics_score",
"Audience Score" = "audience_score",
"Runtime" = "runtime"
),
selected = "audience_score"
),
selectInput(
inputId = "x",
label = "X-axis:",
choices = c(
"IMDB rating" = "imdb_rating",
"IMDB number of votes" = "imdb_num_votes",
"Critics Score" = "critics_score",
"Audience Score" = "audience_score",
"Runtime" = "runtime"
),
selected = "critics_score"
),
selectInput(
inputId = "z",
label = "Color by:",
choices = c(
"Title Type" = "title_type",
"Genre" = "genre",
"MPAA Rating" = "mpaa_rating",
"Critics Rating" = "critics_rating",
"Audience Rating" = "audience_rating"
),
selected = "mpaa_rating"
),
sliderInput(
inputId = "alpha",
label = "Alpha:",
min = 0, max = 1,
value = 0.5
),
sliderInput(
inputId = "size",
label = "Size:",
min = 0, max = 5,
value = 2
),
textInput(
inputId = "plot_title",
label = "Plot title",
placeholder = "Enter text to be used as plot title"
),
actionButton(
inputId = "update_plot_title",
label = "Update plot title"
)
),
mainPanel(
tags$br(),
tags$p(
"These data were obtained from",
tags$a("IMBD", href = "http://www.imbd.com/"), "and",
tags$a("Rotten Tomatoes", href = "https://www.rottentomatoes.com/"), "."
),
tags$p("The data represent", nrow(movies), "randomly sampled movies released between 1972 to 2014 in the United States."),
plotOutput(outputId = "scatterplot")
)
)
)
# Define server ----------------------------------------------------------------
server <- function(input, output, session) {
new_plot_title <- eventReactive(
eventExpr = input$update_plot_title,
valueExpr = {
toTitleCase(input$plot_title)
}
)
output$scatterplot <- renderPlot({
ggplot(data = movies, aes_string(x = input$x, y = input$y, color = input$z)) +
geom_point(alpha = input$alpha, size = input$size) +
labs(title = new_plot_title())
})
}
# Create the Shiny app object --------------------------------------------------
shinyApp(ui = ui, server = server)
```
</details>
### Practice - Add image with the `img` tag
#### Your turn
Now let's practice adding an image to a very simple app.
Note that if you'd like to use a local image for with your app, you'll first have to save the image in a folder `www/` within your root directory. You'll see what we mean when you run the demo in the RStudio Cloud Project. Set a height and width for your image as well.
:::proj
*Navigate to the RStudio Cloud Project titled __4-1b Add image with img tag__ in your RStudio Workspace for the demo*
[<i class="fa fa-cloud"></i> Go to RStudio Cloud Project](https://rstudio.cloud/spaces/81721/join?access_code=I4VJaNsKfTqR3Td9hLP7E1nz8%2FtMg6Xbw9Bgqumv){.btn .test-drive}
:::
```{r ex-4-1b-img, echo = TRUE, eval = FALSE}
# Load packages ----------------------------------------------------------------
library(shiny)
# Define UI --------------------------------------------------------------------
ui <- fluidPage(
titlePanel("An image"),
tags$img(___),
)
# Define server ----------------------------------------------------------------
server <- function(input, output, session) {}
# Create the Shiny app ---------------------------------------------------------
shinyApp(ui = ui, server = server)
```
<details>
<summary>Show solution</summary>
See the following code chunk for the solution to the exercise above.
```{r ex-4-1b-img-solution, echo = TRUE, eval = FALSE}
# Load packages ----------------------------------------------------------------
library(shiny)
# Define UI --------------------------------------------------------------------
ui <- fluidPage(
titlePanel("An image"),
tags$img(height = 100, width = 300, src = "roles_implement.png"),
)
# Define server ----------------------------------------------------------------
server <- function(input, output, session) {}
# Create the Shiny app ---------------------------------------------------------
shinyApp(ui = ui, server = server)
```
</details>
## 4.2 Layout Panels
In this section we discuss the layout of a Shiny app.
### `fluidRow()`
One useful function for customizing the layout of your app is `fluidRow()`. This function creates horizontal rows where objects can be placed.
You can add as many rows as you want, but you want to be careful about expanding your app too much vertically as your users might not be willing to scroll down to interact with your app in full.
```{r, echo = TRUE, eval = FALSE}
library(shiny)
# Define UI with fluid rows
ui <- fluidPage(
"Side", "by", "side", "text",
fluidRow("Text on row 1"),
fluidRow("Text on row 2"),
fluidRow("Text on row 3")
)
# Define server function that does nothing
server <- function(input, output, session) {}
# Create the app object
shinyApp(ui = ui, server = server)
```
---
```{r, eval = TRUE, echo = FALSE}
fluidPage(
"Side", "by", "side", "text",
fluidRow("Text on row 1"),
fluidRow("Text on row 2"),
fluidRow("Text on row 3")
)
```
---
### `column()`
The column function is also incredibly useful. It adds columns within a row, and it requires that you define a width for each column.
```{r, echo = TRUE, eval = FALSE}
library(shiny)
# Define UI with fluid rows and columns
ui <- fluidPage(
fluidRow(
column("R1, C1, width = 3", width = 3),
column("R1, C2, width = 9", width = 9)
),
fluidRow(
column("R2, C1, width = 4", width = 4),
column("R2, C2, width = 4", width = 4),
column("R2, C3, width = 4", width = 4)
)
)
# Define server function that does nothing
server <- function(input, output, session) {}
# Create the app object
shinyApp(ui = ui, server = server)
```
---
```{r, eval = TRUE, echo = FALSE}
fluidPage(
fluidRow(
column("R1, C1, width = 3", width = 3),
column("R1, C2, width = 9", width = 9)
),
fluidRow(
column("R2, C1, width = 4", width = 4),
column("R2, C2, width = 4", width = 4),
column("R2, C3, width = 4", width = 4)
)
)
```
---
### The width is relative
The total width of columns within any given row should add up to 12. You can use the columns to place output objects like a plots or summary tables in specific places in your app.
---
```{r, eval = TRUE, echo = FALSE}
fluidPage(
strong("3 + 9 = 12"),
fluidRow(
column(wellPanel("R1, C1", br(), "width = 3"), width = 3),
column(wellPanel("R1, C2, width = 9"), width = 9)
),
strong("4 + 4 + 4 = 12"),
fluidRow(
column(wellPanel("R2, C1, width = 4"), width = 4),
column(wellPanel("R2, C2, width = 4"), width = 4),
column(wellPanel("R2, C3, width = 4"), width = 4)
)
)
```
---
### Panels
You can use panels to group multiple elements into a single element that has its own properties.
This is especially important and useful for complex apps with a large number of inputs and outputs such that it might not be clear to the user where to get started.
### `wellPanel()`
The `wellpanel()` function groups elements into a grey well, or a box with rounded corners. This is a look you should be used to seeing in shiny apps by now.
```{r, echo = TRUE, eval = FALSE}
library(shiny)
# Define UI with wellPanels
ui <- fluidPage(
wellPanel( fluidRow("Row 1") ),
wellPanel( fluidRow("Row 2") ),
wellPanel( fluidRow("Row 3") )
)
# Define server function that does nothing
server <- function(input, output, session) {}
# Create the app object
shinyApp(ui = ui, server = server)
```
---
```{r, eval = TRUE, echo = FALSE}
fluidPage(
wellPanel( fluidRow("Row 1") ),
wellPanel( fluidRow("Row 2") ),
wellPanel( fluidRow("Row 3") )
)
```
---
### Panels
Shiny offers 12 different panels, but we'll take a look at a small subset of them in this section, and then you'll get to work with a few others in the exercises that follow.
```{r, eval = FALSE}
absolutePanel(...)
fixedPanel(...)
conditionalPanel(...)
headerPanel(...)
mainPanel(...)
navlistPanel(...)
sidebarPanel(...)
tabPanel(...)
tabsetPanel(...)
titlePanel(...)
inputPanel(...)
wellPanel(...)
```
### `sidebarPanel()` and `mainPanel()`
We have made heavy use of `sidebarPanel()` and `mainPanel()` in our apps in this course. However we mostly stuck with their default widths.
The default width for a `sidebarPanel()` is 4 and for a `mainPanel()` is 8.
```{r, echo = TRUE, eval = FALSE}
library(shiny)
# Define UI with default width sidebar
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
strong("Sidebar"),
"Usually inputs go here, width = 4 by default"
),
mainPanel(
strong("Main panel"),
"Usually outputs go here, width = 8 by default"
)
)
)
server <- function(input, output, session) {}
shinyApp(ui = ui, server = server)
```
---
```{r, eval = TRUE, echo = FALSE}
fluidPage(
sidebarLayout(
sidebarPanel(
strong("Sidebar"),
"Usually inputs go here, width = 4 by default"
),
mainPanel(
strong("Main panel"),
"Usually outputs go here, width = 8 by default"
)
)
)
```
---
###
If you wanted to make an app where the `sidebar` and `mainPanel` are of equal width, so the app is split down the middle, you can specify a width argument to the `sidebarPanel()` and `mainPanel()` functions and set each to 6.
```{r, echo = TRUE, eval = FALSE}
library(shiny)
# Define UI with default width sidebar
fluidPage(
sidebarLayout(
sidebarPanel("Sidebar with width = 6", width = 6),
mainPanel("Main panel with width = 6", width = 6)
)
)
server <- function(input, output, session) {}
shinyApp(ui = ui, server = server)
```
---
```{r, eval = TRUE, echo = FALSE}
fluidPage(
sidebarLayout(
sidebarPanel("Sidebar with width = 6", width = 6),
mainPanel("Main panel with width = 6", width = 6)
)
)
```
---
### `titlePanel()`
The `titlePanel()` is used to create a panel containing an application title. Often it makes sense to include this panel outside the `sidebarLayout()`. But in addition to the title on the page, youmight want to also change the text that shows up on the browser tab for our app, especially if our title is long.
```{r, echo = TRUE, eval = FALSE}
library(shiny)
# Define UI with title panel
ui <- fluidPage(
titlePanel("My awesome app"),
sidebarLayout(
sidebarPanel("Some inputs"),
mainPanel("Some outputs")
)
)
server <- function(input, output, session) {}
shinyApp(ui = ui, server = server)
```
---
```{r, eval = TRUE, echo = FALSE}
fluidPage(
titlePanel("My awesome app"),
sidebarLayout(
sidebarPanel("Some inputs"),
mainPanel("Some outputs")
)
)
```
---
### `titlePanel()` with `windowTitle`
To customize this text you specify the `windowTitle` argument in the `titlePanel()`, which is by default equal to the application title. For example, your application title might be "Movie browser, 1970 to 2014", but you might just want to make your window title "Movies".
```{r, echo = TRUE, eval = FALSE}
library(shiny)
# Define UI with title panel and window title
ui <- fluidPage(
titlePanel("Movie browser, 1970 to 2014",
windowTitle = "Movies"),
sidebarLayout(
sidebarPanel("Some inputs"),
mainPanel("Some outputs")
)
)
server <- function(input, output, session) {}
shinyApp(ui = ui, server = server)
```
---
```{r, eval = TRUE, echo = FALSE}
fluidPage(
titlePanel("Movie browser, 1970 to 2014",
windowTitle = "Movies"),
sidebarLayout(
sidebarPanel("Some inputs"),
mainPanel("Some outputs")
)
)
```
---
### `conditionalPanel()`
The last panel we'll consider is `conditionalPanel()`, which creates a panel that is visible conditional upon the value of an input or an output.
First, let's see it in action in the app below. Switch between *any* and *selected* number of digits to see how the sidebar panel changes.
---
```{r, echo = TRUE, eval = FALSE}
fluidPage(
titlePanel("Random number generator"),
sidebarLayout(
sidebarPanel(
selectInput(
"digit_type", "Number of digits:",
c("any", "selected")
),
conditionalPanel(
condition = "input.digit_type == 'selected'",
sliderInput("digit_count", "How many digits?",
min = 1, max = 10, value = 4
)
),
actionButton("go", "Generate new random number"),
width = 5
),
mainPanel(
br(),
"Your random number is",
h4(textOutput("random_number")),
width = 7
)
)
)
```
```{r, context = "server", echo = TRUE, eval = FALSE}
output$random_number <- renderText({
input$go
raw <- runif(1)
digits <- if (input$digit_type == "any") {
sample(1:10, size = 1)
} else {
input$digit_count
}
round(raw * 10^digits)
})
```
---
###
And here is the code that creates the app with the conditional panel.
Under the hood this function evaluates a JavaScript expression once at startup and then whenever Shiny detects a relevant change or input/output.
Being able to display panels conditional on previous user selections is a powerful feature of Shiny.
```{r, echo = TRUE, eval = FALSE}
library(shiny)
# Define UI with conditionalPanel
ui <- fluidPage(