Skip to content

Instantly share code, notes, and snippets.

@mrxiaohe
Created December 17, 2012 17:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrxiaohe/4320327 to your computer and use it in GitHub Desktop.
Save mrxiaohe/4320327 to your computer and use it in GitHub Desktop.
library(shiny)
cnorm <- function(n){
val <- rnorm(n)
uval <- runif(n)
flag <- (uval <= 1 - 0.1)
val[!flag] <- 10 * val[!flag]
val
}
shinyServer(function(input, output){
data <- reactive(function(){
Distribution <- switch(input$Distribution,
Normal = rnorm,
Uniform = runif,
Lognormal = rlnorm,
Exponential = rexp,
"Contaminated Normal" = cnorm,
rnorm
)
Distribution(input$sam_size*2000)
})
output$plot <- reactivePlot(function(){
Distribution <- input$Distribution
sam_size <- input$sam_size
temp <- matrix(data(), ncol=2000)
xbars <- colMeans(temp)
SD <- sd(xbars)
MEAN <- mean(xbars)
ylimits <- range(hist(xbars, br=25)$density, density(xbars)$y)
hist(xbars, br=25, ylim=ylimits,
main=paste("Sampling Distribution of the Mean\nBased on a", Distribution,
"Distribution with n =", sam_size), freq=FALSE)
lines(density(xbars))
curve(dnorm(x, mean=MEAN, sd=SD), add=T, lty=3)
})
})
library(shiny)
# Define UI for application that plots random distributions
shinyUI(pageWithSidebar(
# Application title
headerPanel("Central Limit Theorem"),
# Sidebar with a slider input for number of observations
sidebarPanel(
selectInput("Distribution", "Distribution:",
list("Normal", "Lognormal", "Contaminated Normal", "Exponential")),
br(),
sliderInput("sam_size",
"Sample size:",
min = 5,
max = 500,
value = 5)
),
mainPanel(
tabPanel("Plot", plotOutput("plot")
)
)
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment