-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.R
64 lines (50 loc) · 1.63 KB
/
server.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# This is the server logic for a Shiny web application.
# You can find out more about building applications with Shiny here:
#
# http://shiny.rstudio.com
#
library(shiny)
library(lattice)
library(mclust)
library(ggplot2)
library(dplyr)
# iris without Species
iri <- subset(iris, select = -Species)
iri.pc <- as.data.frame(prcomp(iri, scale. = T)$x)[,c(1,2)]
iri.all <- cbind(iri, iri.pc)
shinyServer(function(input, output) {
AD <- reactive({
# set data
if(input$usepca){
cdata <- as.data.frame(iri.pc)
}else{
cdata <- iri
}
# Cluster analysis
if(input$method == 'mclust'){
class <- factor(Mclust(cdata, G = input$nclust)$classification)
} else if(input$method == 'kmeans'){
class <- factor(kmeans(cdata, input$nclust)$cluster)
} else {
class <- iris$Species
}
class
})
# show the name of the method
output$method <- renderText({paste('Clustering using', input$method)})
# pca or not
output$usepca <- renderText({ input$usepca })
# classification table
output$classtab <- renderTable(xtabs(~iris$Species + AD()), digits = 0)
# Plot the outcome
output$distPlot <- renderPlot({
# create a GGplot
iri.all$class <- AD()
# if plotpca plot principal components
p <- iri.all %>% ggplot() + geom_point(aes_string(x = input$xaxis, y = input$yaxis,
shape = 'iris$Species',
colour = 'class')) +
scale_shape('Species') + scale_color_brewer('Classification',palette = 'Set1') + theme_classic()
p
})
})