Debian Codegraph @

inst/examples-shiny/07_widgets/app.R

raw
1 library(shiny)
2 library(bslib)
3
4 # Define UI for slider demo app ----
5 ui <- page_sidebar(
6
7 # App title ----
8 title = "More Widgets",
9
10 # Sidebar panel for inputs ----
11 sidebar = sidebar(
12
13 # Input: Select a dataset ----
14 selectInput(
15 "dataset",
16 "Choose a dataset:",
17 choices = c("rock", "pressure", "cars")
18 ),
19
20 # Input: Specify the number of observations to view ----
21 numericInput("obs", "Number of observations to view:", 10),
22
23 # Include clarifying text ----
24 helpText(
25 "Note: while the data view will show only the specified",
26 "number of observations, the summary will still be based",
27 "on the full dataset."
28 ),
29
30 # Input: actionButton() to defer the rendering of output ----
31 # until the user explicitly clicks the button (rather than
32 # doing it immediately when inputs change). This is useful if
33 # the computations required to render output are inordinately
34 # time-consuming.
35 actionButton("update", "Update View")
36 ),
37
38 # Output: Header + summary of distribution ----
39 h4("Summary"),
40 verbatimTextOutput("summary"),
41
42 # Output: Header + table of distribution ----
43 h4("Observations"),
44 tableOutput("view")
45 )
46
47 # Define server logic to summarize and view selected dataset ----
48 server <- function(input, output) {
49
50 # Return the requested dataset ----
51 # Note that we use eventReactive() here, which depends on
52 # input$update (the action button), so that the output is only
53 # updated when the user clicks the button
54 datasetInput <- eventReactive(
55 input$update,
56 {
57 switch(
58 input$dataset,
59 "rock" = rock,
60 "pressure" = pressure,
61 "cars" = cars
62 )
63 },
64 ignoreNULL = FALSE
65 )
66
67 # Generate a summary of the dataset ----
68 output$summary <- renderPrint({
69 dataset <- datasetInput()
70 summary(dataset)
71 })
72
73 # Show the first "n" observations ----
74 # The use of isolate() is necessary because we don't want the table
75 # to update whenever input$obs changes (only when the user clicks
76 # the action button)
77 output$view <- renderTable({
78 head(datasetInput(), n = isolate(input$obs))
79 })
80 }
81
82 # Create Shiny app ----
83 shinyApp(ui, server)