| 1 |
library(shiny) |
| 2 |
|
| 3 |
# Define server logic for random distribution app ---- |
| 4 |
function(input, output, session) { |
| 5 |
|
| 6 |
# Reactive expression to generate the requested distribution ---- |
| 7 |
# This is called whenever the inputs change. The output functions |
| 8 |
# defined below then use the value computed from this expression |
| 9 |
d <- reactive({ |
| 10 |
dist <- switch(input$dist, |
| 11 |
norm = rnorm, |
| 12 |
unif = runif, |
| 13 |
lnorm = rlnorm, |
| 14 |
exp = rexp, |
| 15 |
rnorm) |
| 16 |
|
| 17 |
dist(input$n) |
| 18 |
}) |
| 19 |
|
| 20 |
# Generate a plot of the data ---- |
| 21 |
# Also uses the inputs to build the plot label. Note that the |
| 22 |
# dependencies on the inputs and the data reactive expression are |
| 23 |
# both tracked, and all expressions are called in the sequence |
| 24 |
# implied by the dependency graph. |
| 25 |
output$plot <- renderPlot({ |
| 26 |
dist <- input$dist |
| 27 |
n <- input$n |
| 28 |
|
| 29 |
hist(d(), |
| 30 |
main = paste("r", dist, "(", n, ")", sep = ""), |
| 31 |
col = "#75AADB", border = "white") |
| 32 |
}) |
| 33 |
|
| 34 |
# Generate a summary of the data ---- |
| 35 |
output$summary <- renderPrint({ |
| 36 |
summary(d()) |
| 37 |
}) |
| 38 |
|
| 39 |
# Generate an HTML table view of the data ---- |
| 40 |
output$table <- renderTable({ |
| 41 |
d() |
| 42 |
}) |
| 43 |
|
| 44 |
} |