Skip to content

Instantly share code, notes, and snippets.

@ptoche
Forked from jknowles/server.R
Last active January 2, 2016 12:29
Show Gist options
  • Save ptoche/8303959 to your computer and use it in GitHub Desktop.
Save ptoche/8303959 to your computer and use it in GitHub Desktop.
# server.R
library("shiny")
library("ggplot2") # Grammar of Graphics for plots
library("eeptools") # Convenience functions for education data
library("plyr") # Convenience functions to manipulate data
shinyServer(
function(input,output){
POPinput <- reactive({
mypop <- data.frame(
x = rnorm(input$obs)
, y = runif(input$obs)
, z = sample(c("A","B","C","D"), input$obs, replace = TRUE, prob = probs)
)
return(mypop)
})
SAMPinput <- reactive({
switch(input$sampling
, "srs" = POPinput()[sample(row.names(POPinput()),nrow(POPinput()) %/% 5),]
, "cluster" = POPinput()[POPinput()$z %in% sample(unique(POPinput()$z),2),]
, "sys" = as.data.frame(syssamp(POPinput(), 1, 5))
, "strat" = stratified_sampling(POPinput(),"z",size=min(table(POPinput()[,"z"]))%/%2)
)
})
output$distPlot<-renderPlot({
p <- ggplot()
p <- p + geom_point(aes(x = x, y = y), data = POPinput())
p <- p + geom_point(aes(x = x, y = y), data = SAMPinput()
, color = I('blue'), shape = 0, size = I(4))
p <- p + theme_dpi()
p <- p + facet_wrap(~z)
print(p)
})
})
# https://gist.github.com/jknowles/4484845
# ui.R
library("shiny")
library("ggplot2") # Grammar of Graphics for plots
library("eeptools") # Convenience functions for education data
library("plyr") # Convenience functions to manipulate data
shinyUI(
pageWithSidebar(
headerPanel(h3("Sampling Regimes in R"))
,
sidebarPanel(
sliderInput("obs"
, "Size of Population:"
, min = 50
, max = 1000
, value = 100
, step = 50
)
,
selectInput("sampling"
, "Choose a sample type:"
, choices = c("srs", "cluster", "sys","strat")
)
)
,
mainPanel(plotOutput("distPlot"))
)
)
@ptoche
Copy link
Author

ptoche commented Jan 7, 2014

minor edits on an excellent app by jknowles, thanks for sharing!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment