Skip to content

Commit

Permalink
letting users change number of clusters not reactive yet
Browse files Browse the repository at this point in the history
  • Loading branch information
aedobbyn committed Jul 19, 2017
1 parent 489eb8c commit f0266af
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 18 deletions.
26 changes: 13 additions & 13 deletions cluster.R
Original file line number Diff line number Diff line change
Expand Up @@ -84,19 +84,19 @@ resp = response_vars)



cluster_it <- function(df_preds, n_centers) {
set.seed(9)
clustered_df_out <- kmeans(x = df_preds$preds, centers = n_centers, trace = FALSE)
clustered_df <- as_tibble(data.frame(
cluster_assignment = factor(clustered_df_out$cluster),
df_preds$outcome, df_preds$preds,
df_preds$df_for_clustering %>% select(abv, ibu, srm)))
return(clustered_df)
}

clustered_beer <- cluster_it(df_preds = cluster_prep, n_centers = 10)
# cluster_it <- function(df_preds, n_centers) {
# set.seed(9)
# clustered_df_out <- kmeans(x = df_preds$preds, centers = n_centers, trace = FALSE)
#
# clustered_df <- as_tibble(data.frame(
# cluster_assignment = factor(clustered_df_out$cluster),
# df_preds$outcome, df_preds$preds,
# df_preds$df_for_clustering %>% select(abv, ibu, srm)))
#
# return(clustered_df)
# }
#
# clustered_beer <- cluster_it(df_preds = cluster_prep, n_centers = 10)



Expand Down
27 changes: 23 additions & 4 deletions clusterfun/server.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,32 @@ library(shiny)

source("../cluster.R")




# Define server logic required to draw a histogram
shinyServer(function(input, output) {


cluster_it <- function(df_preds, n_centers) {
set.seed(9)
clustered_df_out <- kmeans(x = df_preds$preds, centers = n_centers, trace = FALSE)

clustered_df <- as_tibble(data.frame(
cluster_assignment = factor(clustered_df_out$cluster),
df_preds$outcome, df_preds$preds,
df_preds$df_for_clustering %>% select(abv, ibu, srm)))

return(clustered_df)
}

reactive ({ clustered_beer <- cluster_it(df_preds = cluster_prep, n_centers = input$num_clusters) })



output$cluster_plot <- renderPlot({

this_style_data <- clustered_beer %>% filter(style_collapsed == input$style_collapsed)
this_style_center <- style_centers %>% filter(style_collapsed == input$style_collapsed)
this_style_data <- clustered_beer %>% filter(style_collapsed == input$style_collapsed)
this_style_center <- style_centers %>% filter(style_collapsed == input$style_collapsed)

ggplot() +
geom_point(data = this_style_data,
Expand All @@ -23,7 +42,7 @@ shinyServer(function(input, output) {
ggtitle("k-Means Clustered Beer") +
labs(x = "ABV", y = "IBU") +
labs(colour = "Cluster Assignment") +
theme_minimal()
theme_minimal()

})

Expand Down
7 changes: 6 additions & 1 deletion clusterfun/ui.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ shinyUI(fluidPage(
"Brown" = "Brown",
"Double India Pale Ale" = "Double India Pale Ale",
"Red" = "Red",
"Stout" = "Stout"))
"Stout" = "Stout")),

selectInput("num_clusters", "Number of Clusters:",
list("1" = "1",
"2" = "2",
"10" = "10"))

# checkboxInput("show_centers", "Show style centers", FALSE)
),
Expand Down

0 comments on commit f0266af

Please sign in to comment.