-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.R
378 lines (289 loc) · 11.5 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
# global items
# check if pkgs are installed already, if not, install automatically:
# (http://stackoverflow.com/a/4090208/1036500)
list.of.packages <- c("ggplot2",
"DT",
"GGally",
"psych",
"Hmisc",
"MASS",
"tabplot")
#new.packages <- list.of.packages[!(list.of.packages %in% installed.packages()[,"Package"])]
#if(length(new.packages)) install.packages(new.packages)
# load all these
lapply(list.of.packages, require, character.only = TRUE)
server <- function(input, output) {
# read in the CSV
the_data_fn <- reactive({
inFile <- input$file1
if (is.null(inFile)) return(NULL)
the_data <- read.csv(inFile$datapath, header = (input$header == "Yes"),
sep = input$sep, quote = input$quote, stringsAsFactors=FALSE)
return(the_data)
})
# tableplot
output$tableplot <- renderPlot({
if(is.null(the_data_fn())) return()
the_data <- the_data_fn()
tabplot::tableplot(the_data)
})
# display a table of the CSV contents
output$contents <- DT::renderDataTable({
#
the_data_fn()
})
# display a summary of the CSV contents
output$summary <- renderTable({
the_data <- the_data_fn()
psych::describe(the_data)
})
# Check boxes to choose columns
output$choose_columns_biplot <- renderUI({
the_data <- the_data_fn()
colnames <- names(the_data)
# Create the checkboxes and select them all by default
checkboxGroupInput("columns_biplot", "Choose up to five columns to display on the scatterplot matrix",
choices = colnames,
selected = colnames[1:5])
})
# corr plot
output$corr_plot <- renderPlot({
the_data <- the_data_fn()
# Keep the selected columns
columns_biplot <- input$columns_biplot
the_data_subset_biplot <- the_data[, columns_biplot, drop = FALSE]
ggpairs(the_data_subset_biplot)
})
# corr tables
output$corr_tables <- renderTable({
the_data <- the_data_fn()
# we only want to show numeric cols
the_data_num <- the_data[,sapply(the_data,is.numeric)]
# exclude cols with zero variance
the_data_num <- the_data_num[,!apply(the_data_num, MARGIN = 2, function(x) max(x, na.rm = TRUE) == min(x, na.rm = TRUE))]
res <- Hmisc::rcorr(as.matrix(the_data_num))
cormat <- res$r
pmat <- res$P
ut <- upper.tri(cormat)
df <- data.frame(
row = rownames(cormat)[row(cormat)[ut]],
column = rownames(cormat)[col(cormat)[ut]],
cor = (cormat)[ut],
p = pmat[ut]
)
with(df, df[order(-cor), ])
})
output$bartlett <- renderPrint({
the_data <- the_data_fn()
the_data_num <- na.omit(the_data[,sapply(the_data,is.numeric)])
# exclude cols with zero variance
the_data_num <- the_data_num[,!apply(the_data_num, MARGIN = 2, function(x) max(x, na.rm = TRUE) == min(x, na.rm = TRUE))]
cortest.bartlett(cor(the_data_num), n = nrow(the_data_num))
})
output$kmo <- renderPrint({
the_data <- the_data_fn()
the_data_num <- the_data[,sapply(the_data,is.numeric)]
# exclude cols with zero variance
the_data_num <- the_data_num[,!apply(the_data_num, MARGIN = 2, function(x) max(x, na.rm = TRUE) == min(x, na.rm = TRUE))]
# R <- cor(the_data_num)
# KMO(R)
# http://www.opensubscriber.com/message/[email protected]/7315408.html
# KMO Kaiser-Meyer-Olkin Measure of Sampling Adequacy
kmo = function( data ){
library(MASS)
X <- cor(as.matrix(data))
iX <- ginv(X)
S2 <- diag(diag((iX^-1)))
AIS <- S2%*%iX%*%S2 # anti-image covariance matrix
IS <- X+AIS-2*S2 # image covariance matrix
Dai <- sqrt(diag(diag(AIS)))
IR <- ginv(Dai)%*%IS%*%ginv(Dai) # image correlation matrix
AIR <- ginv(Dai)%*%AIS%*%ginv(Dai) # anti-image correlation matrix
a <- apply((AIR - diag(diag(AIR)))^2, 2, sum)
AA <- sum(a)
b <- apply((X - diag(nrow(X)))^2, 2, sum)
BB <- sum(b)
MSA <- b/(b+a) # indiv. measures of sampling adequacy
AIR <- AIR-diag(nrow(AIR))+diag(MSA) # Examine the anti-image of the
# correlation matrix. That is the
# negative of the partial correlations,
# partialling out all other variables.
kmo <- BB/(AA+BB) # overall KMO statistic
# Reporting the conclusion
if (kmo >= 0.00 && kmo < 0.50){
test <- 'The KMO test yields a degree of common variance
unacceptable for FA.'
} else if (kmo >= 0.50 && kmo < 0.60){
test <- 'The KMO test yields a degree of common variance miserable.'
} else if (kmo >= 0.60 && kmo < 0.70){
test <- 'The KMO test yields a degree of common variance mediocre.'
} else if (kmo >= 0.70 && kmo < 0.80){
test <- 'The KMO test yields a degree of common variance middling.'
} else if (kmo >= 0.80 && kmo < 0.90){
test <- 'The KMO test yields a degree of common variance meritorious.'
} else {
test <- 'The KMO test yields a degree of common variance marvelous.'
}
ans <- list( overall = kmo,
report = test,
individual = MSA,
AIS = AIS,
AIR = AIR )
return(ans)
} # end of kmo()
kmo(na.omit(the_data_num))
})
# Check boxes to choose columns
output$choose_columns_pca <- renderUI({
the_data <- the_data_fn()
# Get the data set with the appropriate name
# we only want to show numeric cols
the_data_num <- na.omit(the_data[,sapply(the_data,is.numeric)])
# exclude cols with zero variance
the_data_num <- the_data_num[,!apply(the_data_num, MARGIN = 2, function(x) max(x, na.rm = TRUE) == min(x, na.rm = TRUE))]
colnames <- names(the_data_num)
# Create the checkboxes and select them all by default
checkboxGroupInput("columns", "Choose columns",
choices = colnames,
selected = colnames)
})
# choose a grouping variable
output$the_grouping_variable <- renderUI({
the_data <- the_data_fn()
# for grouping we want to see only cols where the number of unique values are less than
# 10% the number of observations
grouping_cols <- sapply(seq(1, ncol(the_data)), function(i) length(unique(the_data[,i])) < nrow(the_data)/10 )
the_data_group_cols <- the_data[, grouping_cols, drop = FALSE]
# drop down selection
selectInput(inputId = "the_grouping_variable",
label = "Grouping variable:",
choices=c("None", names(the_data_group_cols)))
})
pca_objects <- reactive({
# Keep the selected columns
columns <- input$columns
the_data <- na.omit(the_data_fn())
the_data_subset <- na.omit(the_data[, columns, drop = FALSE])
# from http://rpubs.com/sinhrks/plot_pca
pca_output <- prcomp(na.omit(the_data_subset),
center = (input$center == 'Yes'),
scale. = (input$scale. == 'Yes'))
# data.frame of PCs
pcs_df <- cbind(the_data, pca_output$x)
return(list(the_data = the_data,
the_data_subset = the_data_subset,
pca_output = pca_output,
pcs_df = pcs_df))
})
output$the_pcs_to_plot_x <- renderUI({
pca_output <- pca_objects()$pca_output$x
# drop down selection
selectInput(inputId = "the_pcs_to_plot_x",
label = "X axis:",
choices= colnames(pca_output),
selected = 'PC1')
})
output$the_pcs_to_plot_y <- renderUI({
pca_output <- pca_objects()$pca_output$x
# drop down selection
selectInput(inputId = "the_pcs_to_plot_y",
label = "Y axis:",
choices= colnames(pca_output),
selected = 'PC2')
})
output$plot2 <- renderPlot({
pca_output <- pca_objects()$pca_output
eig = (pca_output$sdev)^2
variance <- eig*100/sum(eig)
cumvar <- paste(round(cumsum(variance),1), "%")
eig_df <- data.frame(eig = eig,
PCs = colnames(pca_output$x),
cumvar = cumvar)
ggplot(eig_df, aes(reorder(PCs, -eig), eig)) +
geom_bar(stat = "identity", fill = "white", colour = "black") +
geom_text(label = cumvar, size = 4,
vjust=-0.4) +
theme_bw(base_size = 14) +
xlab("PC") +
ylab("Variances") +
ylim(0,(max(eig_df$eig) * 1.1))
})
# PC plot
pca_biplot <- reactive({
pcs_df <- pca_objects()$pcs_df
pca_output <- pca_objects()$pca_output
var_expl_x <- round(100 * pca_output$sdev[as.numeric(gsub("[^0-9]", "", input$the_pcs_to_plot_x))]^2/sum(pca_output$sdev^2), 1)
var_expl_y <- round(100 * pca_output$sdev[as.numeric(gsub("[^0-9]", "", input$the_pcs_to_plot_y))]^2/sum(pca_output$sdev^2), 1)
labels <- rownames(pca_output$x)
grouping <- input$the_grouping_variable
if(grouping == 'None'){
# plot without grouping variable
pc_plot_no_groups <- ggplot(pcs_df,
aes_string(input$the_pcs_to_plot_x,
input$the_pcs_to_plot_y
)) +
geom_text(aes(label = labels), size = 5) +
theme_bw(base_size = 14) +
coord_equal() +
xlab(paste0(input$the_pcs_to_plot_x, " (", var_expl_x, "% explained variance)")) +
ylab(paste0(input$the_pcs_to_plot_y, " (", var_expl_y, "% explained variance)"))
# the plot
pc_plot_no_groups
} else {
# plot with grouping variable
pcs_df$fill_ <- as.character(pcs_df[, grouping, drop = TRUE])
pc_plot_groups <- ggplot(pcs_df, aes_string(input$the_pcs_to_plot_x,
input$the_pcs_to_plot_y,
fill = 'fill_',
colour = 'fill_'
)) +
stat_ellipse(geom = "polygon", alpha = 0.1) +
geom_text(aes(label = labels), size = 5) +
theme_bw(base_size = 14) +
scale_colour_discrete(guide = FALSE) +
guides(fill = guide_legend(title = "groups")) +
theme(legend.position="top") +
coord_equal() +
xlab(paste0(input$the_pcs_to_plot_x, " (", var_expl_x, "% explained variance)")) +
ylab(paste0(input$the_pcs_to_plot_y, " (", var_expl_y, "% explained variance)"))
# the plot
pc_plot_groups
}
})
output$brush_info <- renderTable({
# the brushing function
brushedPoints(pca_objects()$pcs_df, input$plot_brush)
})
# for zooming
output$z_plot1 <- renderPlot({
pca_biplot()
})
# zoom ranges
zooming <- reactiveValues(x = NULL, y = NULL)
observe({
brush <- input$z_plot1Brush
if (!is.null(brush)) {
zooming$x <- c(brush$xmin, brush$xmax)
zooming$y <- c(brush$ymin, brush$ymax)
}
else {
zooming$x <- NULL
zooming$y <- NULL
}
})
# for zooming
output$z_plot2 <- renderPlot({
pca_biplot() + coord_cartesian(xlim = zooming$x, ylim = zooming$y)
})
output$brush_info_after_zoom <- renderTable({
# the brushing function
brushedPoints(pca_objects()$pcs_df, input$plot_brush_after_zoom)
})
output$pca_details <- renderPrint({
#
print(pca_objects()$pca_output$rotation)
summary(pca_objects()$pca_output)
})
output$Colophon <- renderPrint({
})
}