# # This is a Shiny web application. You can run the application by clicking # the 'Run App' button above. # # Find out more about building applications with Shiny here: # # http://shiny.rstudio.com/ # library(shiny) library(ggplot2) # Define UI for random distribution app ---- ui <- fluidPage( sidebarPanel( # Input: Select the random distribution type ---- radioButtons("dist", "Distribution type:", c("Normal" = "norm", "Unif" = "unif", "Binomial" = "binom", "Poisson" = "pois", "Chi-square" = "chisq", "F" = "f", "T" = "t") ), sliderInput("n", "Sample Size", min = 1, max = 10000, value = 50), sliderInput("bins", "Histogram bin count:", min = 1, max = 300, value = 30), sliderInput("bw", "Density Bandwidth", min = 0, max = 1, value = 0.5), # Normal distribution parameters conditionalPanel( condition = "input.dist == 'norm'", sliderInput("mean", "mean", min = -100, max = 100, value = 0), sliderInput("sdev", "sdev", min = 0, max = 100, value = 1) ), # Normal distribution parameters conditionalPanel( condition = "input.dist == 'unif'", sliderInput("range", "Minimum and Maximum", min = -100, max = 100, value = c(0, 10) ) ), conditionalPanel( condition = "input.dist == 'binom'", sliderInput("size", "Number of trials:", min = 1, max = 10000, value = 1), sliderInput("prob", "Probability of success:", min = 0, max = 1, value = 0.5) ), conditionalPanel( condition = "input.dist == 'pois'", sliderInput("lambda", "Lambda (Mean occurrence per interval):", min = 0, max = 1000, value = 10) ), conditionalPanel( condition = "input.dist == 'chisq'", sliderInput("df", "Degrees of freedom:", min = 1, max = 100, value = 2) ), conditionalPanel( condition = "input.dist == 'f'", sliderInput("df1", "Degrees of freedom 1:", min = 1, max = 100, value = 1), sliderInput("df2", "Degrees of freedom 2:", min = 1, max = 100, value = 1) ), conditionalPanel( condition = "input.dist == 't'", sliderInput("df", "Degrees of freedom:", min = 1, max = 100, value = 2) )), mainPanel( # Output: Tabset w/ plot, summary, and table ---- plotOutput("histPlot") ) ) # Define server logic required to draw a histogram server <- function(input, output) { output$histPlot <- renderPlot({ random_sample <- data.frame(switch(input$dist, 'norm' = rnorm(n = input$n, mean = input$mean, sd = input$sdev), 'unif' = runif(n = input$n, min = input$range[1], max = input$range[2]), 'binom' = rbinom(n = input$n, size = input$size, prob = input$prob), 'pois' = rpois(n = input$n, lambda = input$lambda), 'chisq' = rchisq(n = input$n, df = input$df), 'f' = rf(n = input$n, df1 = input$df1, df2 = input$df2), 't' = rt(n = input$n, df = input$df) )) quant_vect <- seq(min(random_sample), max(random_sample), (max(random_sample) - min(random_sample))/ 100) plot_pdf <- data.frame(switch(input$dist, 'norm' = dnorm(quant_vect, mean = input$mean, sd = input$sdev), 'unif' = dunif(quant_vect, min = input$range[1], max = input$range[2]), 'binom' = dbinom(round(quant_vect), size = input$size, prob = input$prob), 'pois' = dpois(round(quant_vect), lambda = input$lambda), 'chisq' = dchisq(quant_vect, df = input$df), 'f' = df(quant_vect, df1 = input$df1, df2 = input$df2), 't' = dt(quant_vect, df = input$df) )) pdf_frame <- data.frame(quant_vect, plot_pdf) names(pdf_frame) <- c('quants', 'probs') names(random_sample) <- c('x') ggplot(data = random_sample, aes(x = x)) + geom_histogram(aes(y=..density..), bins = input$bins) + geom_vline(xintercept = mean(random_sample$x), color = 'red', linetype = 'dotted', size = 1) + geom_density(bw = input$bw) + geom_line(aes(x = quants, y = probs), color = 'red', data = pdf_frame) }) } # Run the application shinyApp(ui = ui, server = server)