John Theodore
August 23, 2019
This shiny app produces a graphical output of K-Means cluster analysis on the mtcars data set.
Input values that the user generates include:
The graphical output generated by the user input includes:
library(shiny)
shinyUI(pageWithSidebar(
headerPanel("MTCARS Cluster Analysis"),
sidebarPanel(
sliderInput('k', h5("1. Choose number of clusters"), value = 2,
min = 1, max = 7, step =1),
textInput("textx", h5("2. Type name of feature for x axis (e.g., mpg, hp, disp, wt, qsec, drat)"), value = "mpg"),
textInput("texty", h5("3. Type name of feature for y axis (e.g., mpg, hp, disp, wt, qsec, drat)"), value = "hp"),
numericInput("nstart", h5("4. Type number of nstarts"), value = 20),
numericInput("itermax", h5("5. Type number of maximum iterations"), value = 50)
),
mainPanel(
h3("K-Means Cluster Groups"),
tableOutput(("wss")),
plotOutput('newCluster'),
plotOutput("screePlot")
)
))
<!–html_preserve–>
library(UsingR)
library(dplyr)
library(factoextra)
data(mtcars)
shinyServer(function(input, output) {
output$newCluster <- renderPlot({
km.out <- kmeans(mtcars, input$k, input$nstart, input$itermax)
fviz_cluster(km.out, mtcars[, c(input$textx, input$texty)])
}, height = 400, width = 700)
output$screePlot <- renderPlot({
fviz_nbclust(mtcars, kmeans, method = "wss")
}, height = 200, width = 700)
output$wss <- renderText({
km.out <- kmeans(mtcars, input$k, input$nstart, input$itermax)
paste(input$k, "Clusters", "with total within sum of squares of:",
round(km.out$tot.withinss))
})
})