-
-
Save jcheng5/9493762 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
Title: Dynamic Clustering | |
Author: Jeff Allen <jeff@rstudio.com> | |
AuthorUrl: http://www.rstudio.com/ | |
License: MIT | |
DisplayMode: Showcase | |
Tags: mclust clustering clickid observe isolate | |
Type: Shiny |
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
library(shiny) | |
library(mclust) | |
shinyServer(function(input, output, session) { | |
# Create a spot where we can store additional | |
# reactive values for this session | |
val <- reactiveValues(x=NULL, y=NULL) | |
# Listen for clicks | |
observe({ | |
# Initially will be empty | |
if (is.null(input$clusterClick)){ | |
return() | |
} | |
isolate({ | |
val$x <- c(val$x, input$clusterClick$x) | |
val$y <- c(val$y, input$clusterClick$y) | |
}) | |
}) | |
# Count the number of points | |
output$numPoints <- renderText({ | |
length(val$x) | |
}) | |
# Clear the points on button click | |
observe({ | |
if (input$clear > 0){ | |
val$x <- NULL | |
val$y <- NULL | |
} | |
}) | |
# Generate the plot of the clustered points | |
output$clusterPlot <- renderPlot({ | |
tryCatch({ | |
# Format the data as a matrix | |
data <- matrix(c(val$x, val$y), ncol=2) | |
# Try to cluster | |
if (length(val$x) <= 1){ | |
stop("We can't cluster less than 2 points") | |
} | |
suppressWarnings({ | |
fit <- Mclust(data) | |
}) | |
mclust2Dplot(data = data, what = "classification", | |
classification = fit$classification, identify = FALSE, | |
xlim=c(-2,2), ylim=c(-2,2)) | |
}, error=function(warn){ | |
# Otherwise just plot the points and instructions | |
plot(val$x, val$y, xlim=c(-2, 2), ylim=c(-2, 2), xlab="", ylab="") | |
text(0, 0, "Unable to create clusters.\nClick to add more points.") | |
}) | |
}) | |
}) |
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
library(shiny) | |
shinyUI( | |
# Create a bootstrap fluid layout | |
fluidPage( | |
# Add a title | |
titlePanel("Dynamic Clustering in Shiny"), | |
# Add a row for the main content | |
fluidRow( | |
# Create a space for the plot output | |
plotOutput( | |
"clusterPlot", "100%", "500px", clickId="clusterClick" | |
) | |
), | |
# Create a row for additional information | |
fluidRow( | |
# Take up 2/3 of the width with this element | |
mainPanel("Points: ", verbatimTextOutput("numPoints")), | |
# And the remaining 1/3 with this one | |
sidebarPanel(actionButton("clear", "Clear Points")) | |
) | |
) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment