Skip to content

Instantly share code, notes, and snippets.

@spsaaibi
Created December 4, 2012 13:04
Show Gist options
  • Save spsaaibi/4203678 to your computer and use it in GitHub Desktop.
Save spsaaibi/4203678 to your computer and use it in GitHub Desktop.
Simple 2d_density_plot
require(shiny)
require(ggplot2)
nObs <- 500
nCluster <- 7
dataset <- data.frame(X = rnorm(nObs) + 2 * rnorm(nObs/2) , Y = rnorm(nObs)) - 2 * rnorm(nObs/2)
kMeans <- kmeans(dataset, centers = nCluster)
dataset$Cluster <- as.factor(kMeans$cluster)
shinyServer(function(input, output) {
dataset <- reactive(function() {
val <- data.frame(X = rnorm(input$nObs) + 2 * rnorm(input$nObs/2) , Y = rnorm(input$nObs)) - 2 * rnorm(input$nObs/2)
kMeans <- kmeans(val, centers = input$nCluster)
val$Cluster <- as.factor(kMeans$cluster)
val
})
output$plot <- reactivePlot(function() {
p <- ggplot(dataset) + geom_point(aes(x = input$X, y = input$Y, colour = input$Cluster))
print(p)
}, height=700)
})
# library(shiny)
# library(ggplot2)
#
# nObs <- 5000
# dataset <- rnorm(nObs)
#
# shinyServer(function(input, output) {
#
# dataset <- reactive(function() {
# rnorm(input$nObs)
# })
#
# output$plot <- reactivePlot(function() {
#
# plot(rnorm(input$nObs))
#
# }, height=700)
#
# })
library(shiny)
library(ggplot2)
shinyUI(pageWithSidebar(
headerPanel("2d statistics visualization"),
sidebarPanel(
sliderInput('nObs', 'nObs', min=10, max=10000,
value=min(10, 10000), step=10, round=0),
sliderInput('nCluster', 'nCluster', min=1, max=10,
value=min(1, 10), step=1, round=0)
),
mainPanel(
plotOutput('plot')
)
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment