Skip to content

Instantly share code, notes, and snippets.

@ptoche
Forked from jknowles/server.R
Last active January 2, 2016 12:19
Show Gist options
  • Save ptoche/8302280 to your computer and use it in GitHub Desktop.
Save ptoche/8302280 to your computer and use it in GitHub Desktop.
# server.R
library("shiny")
library("ggplot2") # Grammar of Graphics for plots
library("VGAM") # Vector Generalized Linear and Additive Models
library("eeptools") # Convenience functions for education data
shinyServer(
function(input,output){
mydat <- reactive({
mydat <- rskewnorm( # old name rsnorm()
input$obs
, location = input$mean
, scale = input$variance
, shape = input$skew
)
if(input$mode==input$mean){
return(mydat)
} else if (input$mode!=input$mean) {
a <- table(as.vector(round(mydat)))
m <- names(a)[a==max(a)]
v <- max(a)
mydat2 <- mydat[round(mydat)!=as.numeric(m)]
samp <- sample(mydat[round(mydat)==as.numeric(m)],v*0.8)
mydat2 <- c(mydat2,samp)
fill <- rep(input$mode,(v-length(samp)))
mydat <- c(mydat2,fill)
return(mydat)
}
})
output$distPlot<-renderPlot({
p <- qplot(mydat(), geom = 'blank')
p <- p + geom_line(aes(y = ..density.., colour = 'Empirical'), stat = 'density')
p <- p + stat_function(fun = dnorm, aes(colour = 'Normal'))
p <- p + geom_histogram(aes(y = ..density..), alpha = 0.4, binwidth = 0.2)
p <- p + scale_colour_manual(name = 'Density', values = c('red', 'blue'))
p <- p + theme_dpi()
p <- p + theme(legend.position = c(0.85, 0.85))
p <- p + xlim(c(-10,10))
p <- p + labs(x = "data", y = "density", title = "Distribution of Data")
print(p)
})
})
# https://gist.github.com/dgrapov/6147592
# https://gist.github.com/jknowles/4484886
# ui.R
library("shiny")
library("ggplot2") # Grammar of Graphics for plots
library("VGAM") # Vector Generalized Linear and Additive Models
library("eeptools") # Convenience functions for education data
shinyUI(
pageWithSidebar(
headerPanel("Exploring Properties of Distributions")
,
sidebarPanel(
sliderInput("obs"
, "Number of tries:"
, min = 200
, max = 5000
, value = 500
, step = 250
)
,
sliderInput("mean"
, "Mean of the Distribution"
, min = -10
, max = 10
, value = 0
, step = 1
)
,
sliderInput("mode"
, "Mode of the Distribution"
, min = -10
, max = 10
, value = 0
, step = 1
)
,
sliderInput("variance"
, "Variance of the Distribution"
, min = 1
, max = 5
, value = 1
, step = 1
)
,
sliderInput("skew"
, "skew of the Distribution"
, min = -5
, max = 5
, value = 0
, step = 1
)
)
,
mainPanel(plotOutput("distPlot"))
)
)
@ptoche
Copy link
Author

ptoche commented Jan 7, 2014

played around with app by jknowles, noticed rsnorm(), from package VGAM, appears to have been renamed rskewnorm(), the above is updated to reflect that, very nice apps, thanks for sharing!

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