Skip to content

Instantly share code, notes, and snippets.

@rpruim
Created November 21, 2012 19:19
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rpruim/4127030 to your computer and use it in GitHub Desktop.
Save rpruim/4127030 to your computer and use it in GitHub Desktop.
Shiny Sampling Distributions
library(shiny)
library(mosaic)
require(lattice)
require(datasets)
# Define server logic required to generate and plot a random distribution
shinyServer(
function(input, output) {
popDist <- reactive( function() input$popDist )
input$n <- reactive( function() input$n )
params <- reactive( function() {
switch(popDist(),
"normal" = list(mean=input$mean, sd=input$sd),
"beta" = list(shape1=input$shape1, shape2=input$shape2),
"gamma" = list(shape=input$shape, scale=input$scale),
"weibull" = list(shape=input$shape, scale=input$scale)
)} )
rdist <- reactive( function() {
switch(popDist(),
"normal" = "rnorm",
"beta" = "rbeta",
"gamma" = "rgamma",
"weibull" = "rweibull"
)} )
distName <- reactive( function() {
switch(popDist(),
"normal" = "norm",
"beta" = "beta",
"gamma" = "gamma",
"weibull" = "weibull"
)} )
samplingDist <- reactive( function()
replicate(5000, mean(
do.call(rdist(), c( list(input$n), params() ))
))
)
output$mainText <- reactive(function(){ paste( "Population:", popDist() ) })
output$samplingDistHeader <- reactive(function(){
paste( "Sampling Distribution for Sample Means (n=", as.character(input$n), ")", sep="")
})
output$populationPlot <- reactivePlot(function() {
print(plotDist( distName() , params=params(), par.settings=col.mosaic() ))
})
output$histogram<- reactivePlot(function() {
print(xhistogram( ~ samplingDist(), par.settings=col.mosaic() ))
})
output$densityplot<- reactivePlot(function() {
print(densityplot( ~ samplingDist(), par.settings=col.mosaic() ))
})
output$qqPlot<- reactivePlot(function() {
print(qqmath( samplingDist(), par.settings=col.mosaic() ))
})
})
library(shiny)
shinyUI(pageWithSidebar(
# Application title
headerPanel("Sampling Distributions"),
# Sidebar with a slider input for number of observations
sidebarPanel(
sliderInput("n",
"Sample size:",
min = 1,
max = 400,
value = 2),
selectInput("popDist", "Population",
list("Normal" = "normal",
"Beta" = "beta",
"Gamma" = "gamma",
"Weibull" = "weibull"
)
), # end selectInput
conditionalPanel(
condition = "input.popDist == 'beta'",
sliderInput("shape1", "shape1: ", min=0.1, max=10, value=1, step=0.05),
sliderInput("shape2", "shape2: ", min=0.1, max=10, value=1, step=0.05)
),
conditionalPanel(
condition = "input.popDist == 'gamma'",
sliderInput("shape", "shape: ", min=0.1, max=10, value=1, step=0.05),
sliderInput("scale", "scale: ", min=0.1, max=10, value=1, step=0.05)
),
conditionalPanel(
condition = "input.popDist == 'weibull'",
sliderInput("shape", "shape: ", min=0.1, max=10, value=1, step=0.05),
sliderInput("scale", "scale: ", min=0.1, max=10, value=1, step=0.05)
),
conditionalPanel(
condition = "input.popDist == 'normal'",
sliderInput("mean", "mean: ", min=0, max=400, value=0),
sliderInput("sd", "st. dev.: ", min=0.1, max=20, value=1, step=.1)
)
), # end sidebarPanel
mainPanel(
h3(textOutput("mainText")),
plotOutput("populationPlot",width="250px", height="180px"),
h3(textOutput("samplingDistHeader")),
tabsetPanel(
tabPanel("Histogram", plotOutput('histogram',height="220px",width="500px")),
tabPanel("Density plot", plotOutput('densityplot', height="220px",width="500px")),
tabPanel("Normal quantile plot", plotOutput('qqPlot', height="220px",width="500px"))
)
)
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment