August 20, 2018

Introduction

This presentation is part of the assignment for week 4 of Coursera's Developing Data Product course. This presentation will showcase a Shiny app developed to discover the k-means cluster of the set of cars across different variables.
The app allows the user to manipulate the x and y input as well as the number of clusters.
The output will generate the k means cluster calculation through a graph display based on the user's input.
The dataset used is from the CRAN library "mtcars" dataset.

Application

R code

Overview of the code

library(shiny)

ui <- fluidPage(

pageWithSidebar(
  headerPanel('Cars K-means clustering'),
  sidebarPanel(
    selectInput('xcol', 'X Variable', names(mtcars)),
    selectInput('ycol', 'Y Variable', names(mtcars),
                selected=names(mtcars)[[2]]),
    numericInput('clusters', 'Cluster count', 3,
                 min = 1, max = 9)
  ),
  mainPanel(
    plotOutput('plot1'),
    textOutput(outputId = "desc")
  )
  
)
  
)

server <- function(input, output, session) {
# Combine the selected variables into a new data frame
  selectedData <- reactive({
    mtcars[, c(input$xcol, input$ycol)]
  })
  
  clusters <- reactive({
    kmeans(selectedData(), input$clusters)
  })
  
  output$plot1 <- renderPlot({
    
    par(mar = c(5.1, 4.1, 0, 1))
    plot(selectedData(),
         col = clusters()$cluster,
         pch = 20, cex = 3)
    points(clusters()$centers, pch = 4, cex = 4, lwd = 4)
  })
  output$desc <- renderText({
    "Documentation can be found at:https://github.com/whyler12/ddp"
  })
}  


shinyApp(ui, server)
## PhantomJS not found. You can install it with webshot::install_phantomjs(). If it is installed, please make sure the phantomjs executable can be found via the PATH variable.

Shiny App

How the App interface looks like