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