# # 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") ), 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) ) ) ), 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]) ) ) 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) # + #stat_function(fun = dnorm, n = 101, args = list(mean = 0, sd = 1), color = 'red') }) } # Run the application shinyApp(ui = ui, server = server)