Debian Codegraph @

inst/examples/06_tabsets/app.R

raw
1 library(shiny)
2
3 # Define UI for random distribution app ----
4 ui <- fluidPage(
5
6 # App title ----
7 titlePanel("Tabsets"),
8
9 # Sidebar layout with input and output definitions ----
10 sidebarLayout(
11
12 # Sidebar panel for inputs ----
13 sidebarPanel(
14
15 # Input: Select the random distribution type ----
16 radioButtons("dist", "Distribution type:",
17 c("Normal" = "norm",
18 "Uniform" = "unif",
19 "Log-normal" = "lnorm",
20 "Exponential" = "exp")),
21
22 # br() element to introduce extra vertical spacing ----
23 br(),
24
25 # Input: Slider for the number of observations to generate ----
26 sliderInput("n",
27 "Number of observations:",
28 value = 500,
29 min = 1,
30 max = 1000)
31
32 ),
33
34 # Main panel for displaying outputs ----
35 mainPanel(
36
37 # Output: Tabset w/ plot, summary, and table ----
38 tabsetPanel(type = "tabs",
39 tabPanel("Plot", plotOutput("plot")),
40 tabPanel("Summary", verbatimTextOutput("summary")),
41 tabPanel("Table", tableOutput("table"))
42 )
43
44 )
45 )
46 )
47
48 # Define server logic for random distribution app ----
49 server <- function(input, output) {
50
51 # Reactive expression to generate the requested distribution ----
52 # This is called whenever the inputs change. The output functions
53 # defined below then use the value computed from this expression
54 d <- reactive({
55 dist <- switch(input$dist,
56 norm = rnorm,
57 unif = runif,
58 lnorm = rlnorm,
59 exp = rexp,
60 rnorm)
61
62 dist(input$n)
63 })
64
65 # Generate a plot of the data ----
66 # Also uses the inputs to build the plot label. Note that the
67 # dependencies on the inputs and the data reactive expression are
68 # both tracked, and all expressions are called in the sequence
69 # implied by the dependency graph.
70 output$plot <- renderPlot({
71 dist <- input$dist
72 n <- input$n
73
74 hist(d(),
75 main = paste("r", dist, "(", n, ")", sep = ""),
76 col = "#75AADB", border = "white")
77 })
78
79 # Generate a summary of the data ----
80 output$summary <- renderPrint({
81 summary(d())
82 })
83
84 # Generate an HTML table view of the data ----
85 output$table <- renderTable({
86 d()
87 })
88
89 }
90
91 # Create Shiny app ----
92 shinyApp(ui, server)