-
-
Save ptoche/8303959 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) | |
}) | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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")) | |
) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
minor edits on an excellent app by jknowles, thanks for sharing!