File: app.R

package info (click to toggle)
r-cran-shiny 1.10.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 10,948 kB
  • sloc: javascript: 39,934; sh: 28; makefile: 20
file content (83 lines) | stat: -rw-r--r-- 2,201 bytes parent folder | download
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
library(shiny)
library(bslib)

# Define UI for slider demo app ----
ui <- page_sidebar(

  # App title ----
  title = "More Widgets",

  # Sidebar panel for inputs ----
  sidebar = sidebar(

    # Input: Select a dataset ----
    selectInput(
      "dataset",
      "Choose a dataset:",
      choices = c("rock", "pressure", "cars")
    ),

    # Input: Specify the number of observations to view ----
    numericInput("obs", "Number of observations to view:", 10),

    # Include clarifying text ----
    helpText(
      "Note: while the data view will show only the specified",
      "number of observations, the summary will still be based",
      "on the full dataset."
    ),

    # Input: actionButton() to defer the rendering of output ----
    # until the user explicitly clicks the button (rather than
    # doing it immediately when inputs change). This is useful if
    # the computations required to render output are inordinately
    # time-consuming.
    actionButton("update", "Update View")
  ),

  # Output: Header + summary of distribution ----
  h4("Summary"),
  verbatimTextOutput("summary"),

  # Output: Header + table of distribution ----
  h4("Observations"),
  tableOutput("view")
)

# Define server logic to summarize and view selected dataset ----
server <- function(input, output) {

  # Return the requested dataset ----
  # Note that we use eventReactive() here, which depends on
  # input$update (the action button), so that the output is only
  # updated when the user clicks the button
  datasetInput <- eventReactive(
    input$update,
    {
      switch(
        input$dataset,
        "rock" = rock,
        "pressure" = pressure,
        "cars" = cars
      )
    },
    ignoreNULL = FALSE
  )

  # Generate a summary of the dataset ----
  output$summary <- renderPrint({
    dataset <- datasetInput()
    summary(dataset)
  })

  # Show the first "n" observations ----
  # The use of isolate() is necessary because we don't want the table
  # to update whenever input$obs changes (only when the user clicks
  # the action button)
  output$view <- renderTable({
    head(datasetInput(), n = isolate(input$obs))
  })
}

# Create Shiny app ----
shinyApp(ui, server)