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/8303340 to your computer and use it in GitHub Desktop.
Save ptoche/8303340 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
# static function
rnormcor <- function(x,rho) {rnorm(1,rho*x,sqrt(1-rho^2))}
shinyServer(
function(input,output){
output$distPlot <- renderPlot({
a <- rnorm(input$obs)
b <- sapply(a, rnormcor, rho = input$rho)
p <- qplot(a, b, alpha = 0.5)
p <- p + scale_alpha(guide = "none")
p <- p + geom_smooth(method = "lm", se = TRUE, level = .99
, fill = "blue", colour = "darkblue", alpha = 0.2, size = 1)
p <- p + theme_dpi()
p <- p + labs(x = "", y = "", title = paste0("Correlation = ", input$rho, ", Confidence Level = .99"))
# p <- p + geom_text(aes(x = -1, y = 3, label = paste0("Corr. = ", input$rho)), size = 8)
print(p)
})
})
# https://gist.github.com/jknowles/4484910
# ui.R
library("shiny")
library("ggplot2") # Grammar of Graphics for plots
library("eeptools") # Convenience functions for education data
shinyUI(
pageWithSidebar(
headerPanel(h3("Exploring Properties of Distributions"))
,
sidebarPanel(
sliderInput("obs"
, "Number of observations:"
, min = 200
, max = 5000
, value = 500
, step = 250
)
, br(), br(),
sliderInput("rho"
, "Correlation Coefficient"
, min = -1
, max = 1
, value = 0
, step = 0.1
)
)
,
mainPanel(plotOutput("distPlot"))
)
)
@ptoche
Copy link
Author

ptoche commented Jan 7, 2014

very nice app by jknowles, thanks for sharing! marginal edits above

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