Pascal Gr.
06.28.2018
Explore the mtcars dataset and cluster data by using the k-means method. Three input fields are provided to compare two variables of the dataset and put them on the x and y axis. The third input field defines the number of cluster to be generated.
library(shiny)
pageWithSidebar(
headerPanel('MT 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 = 6)
),
mainPanel(
plotOutput('plot1')
)
)
<!–html_preserve–>
library(shiny)
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({
palette(c("yellow3", "olivedrab3", "maroon4", "steelblue4",
"tomato3"))
par(mar = c(5.1, 4.1, 0, 1))
plot(selectedData(),
col = clusters()$cluster,
pch = 20, cex = 4)
points(clusters()$centers, pch = 4, cex = 4, lwd = 4)
})
}
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({
palette(c("yellow3", "olivedrab3", "maroon4", "steelblue4",
"tomato3"))
par(mar = c(5.1, 4.1, 0, 1))
plot(selectedData(),
col = clusters()$cluster,
pch = 20, cex = 4)
points(clusters()$centers, pch = 4, cex = 4, lwd = 4)
})
}
[1] "https://pascal226.shinyapps.io/LastOne/"