-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.R
385 lines (295 loc) · 12.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
379
380
381
382
383
384
385
######################################
## Author: Riti Kumari (sinhariti61@gmail.com)
## Date: 2018-02-13
## Title: Socail Network Analysis
## Purpose: Analysis of EU Employee datasets
# Analysis & visualization of employee and department level interaction
# Visualization of the degree centrality , in-degree, out-degree and betweeness
# Depiction of the important persons of the organisation
# Analysis of the horizaontality of the organization
######################################
library(shiny)
library(igraph)
library(networkD3)
library(dplyr)
shinyServer(function(input,output)
{
# Reading the uploaded file EU_Core
dataset = reactive({
infile = input$file1
if (is.null(infile))
return(NULL)
infile_read = read.table(infile$datapath,sep = "\t", header=FALSE)
colnames(infile_read) <- c ("Sender","Receiver")
return(infile_read)
})
# Reading the uploaded file EU_Department
dataset2 = reactive({
infile = input$file2
if (is.null(infile))
return(NULL)
infile_read = read.table(infile$datapath,sep = "\t", header=FALSE)
colnames(infile_read) <- c ("Code","Department")
return(infile_read)
})
############
SenderFreqDataset = reactive({
data_freq= as.data.frame(table(dataset()[,1]))
names(data_freq)= c("Code", "Frequency")
data_freq
})
ReceiverFreqDataset = reactive({
data_freq= as.data.frame(table(dataset()[,2]))
names(data_freq)= c("Code","Frequency")
data_freq
})
## Degree Centrality
calcentrality <- reactive({
if(is.null(dataset())){return ()}
CoreData_df <- dataset()
graph <- graph_from_data_frame(CoreData_df ,directed = T)
#Making a data.frame from graph
calDeg <- as.data.frame.character(V(graph))
calDeg$Rowname <-row.names(calDeg)
#calculating degree centrality
calDeg$Degree <- igraph::degree(graph, mode="all")
#sorting with respect to centrality and selecting the top ten nodes.
calDeg <- calDeg[with(calDeg, order(-Degree)),]
calDeg
})
##################
##BetweenNess
calbetween <-reactive({
if(is.null(dataset())){return ()}
CoreData_df <- dataset()
graph <- graph_from_data_frame(CoreData_df ,directed = T)
#Making a data.frame from graph
calBet <- as.data.frame.character(V(graph))
calBet$Rowname <-row.names(calBet)
#calculating betweenness
calBet$Betweenness <- igraph::betweenness(graph)
#sorting with respect to betweenness and selecting the top ten nodes.
calBet <- calBet[with(calBet, order(-Betweenness)),]
calBet
})
#################
##Calculation of Indegree Centrality
calindegree <- reactive({
if(is.null(dataset())){return ()}
CoreData_df <- dataset()
graph <- graph_from_data_frame(CoreData_df ,directed = T)
#Making a data.frame from graph
calDeg <- as.data.frame.character(V(graph))
calDeg$Rowname <-row.names(calDeg)
#calculating degree centrality
calDeg$Degree <- igraph::degree(graph, mode="in")
#sorting with respect to centrality and selecting the top ten nodes.
calInDeg <- calDeg[with(calDeg, order(-Degree)),]
calInDeg
})
################
connectionCount <- reactive({input$hopCount})
#Sending the output
#First 50 rows for the first uploaded file
output$tb1 <- renderDataTable({
dataset()
})
#First 50 rows for the 2nd uploaded file
output$tb2 <- renderDataTable({
dataset2()
})
#Plotting the n/w
output$plot <- renderSimpleNetwork({
count <- as.numeric(connectionCount()[1])
networkdata <- head(dataset(),count)
simpleNetwork(networkdata, linkDistance = 50, charge = -30, fontSize = 15, fontFamily = "serif",
linkColour = "Black", nodeColour = "Red", opacity = 0.6, zoom = F)
})
############
#Counting the emails sent and displaying it in UI
output$sender <- renderDataTable({
data_freq= as.data.frame(table(dataset()[,1]))
names(data_freq)= c("Employee Code","Emails Sent")
data_freq
})
############
#Counting the emails received and displaying it in UI
output$receiver <- renderDataTable({
data_freq= as.data.frame(table(dataset()[,2]))
names(data_freq)= c("Employee Code","Emails Received")
data_freq
})
############
#2-hop Sender Connection
output$ShopConnection <- renderSimpleNetwork({
data_freq= as.data.frame(table(dataset()[,1]))
names(data_freq)= c("Sender","Frequency")
#Ordering the dataset with frequency and taking the top 10 sender
data_freq = data_freq[with(data_freq, order(-Frequency)), ]
SenderList <- head(data_freq,input$topN)
#Expansion of the SenderList
SenderMailList <- filter(dataset(),Sender %in% SenderList$Sender)
#1st hop
FirstReceiver <- unique(SenderMailList$Receiver)
SenderVec <- SenderList$Sender
FinalSenderList <- append(SenderVec,FirstReceiver)
#2nd hop
SenderMailList <- filter(dataset(),Sender %in% FinalSenderList)
SenderMailList <- unique(SenderMailList)
#SenderMailList
simpleNetwork(head(SenderMailList,250), linkDistance = 50, charge = -30, fontSize = 15, fontFamily = "serif",
linkColour = "Black", nodeColour = "Red", opacity = 0.6, zoom = F)
})
############
#2-hop Receiver Connection
output$RhopConnection <- renderSimpleNetwork({
data_freq= as.data.frame(table(dataset()[,2]))
names(data_freq)= c("Receiver","Frequency")
#Ordering the dataset with frequency and taking the top 10 Receiver
data_freq = data_freq[with(data_freq, order(-Frequency)), ]
ReceiverList <- head(data_freq,input$topN)
#Expansion of the ReceiverList
ReceiverMailList <- filter(dataset(),Receiver %in% ReceiverList$Receiver)
#1st hop
FirstSender <- unique(ReceiverMailList$Sender)
ReceiverVec <- ReceiverList$Receiver
FinalReceiverList <- append(ReceiverVec,FirstSender)
#2nd hop
ReceiverMailList <- filter(dataset(),Receiver %in% FinalReceiverList)
ReceiverMailList <- unique(ReceiverMailList)
simpleNetwork(head(ReceiverMailList,250),linkDistance = 50, charge = -30, fontSize = 15, fontFamily = "serif",
linkColour = "Black", nodeColour = "Red", opacity = 0.6, zoom = F)
})
############
#Calculating centrality
output$DegreeCentrality <- renderDataTable({
calcentrality()
})
#Plotting Degree Centrality
output$centralityPlot <-renderForceNetwork({
centralityData <- head(calcentrality()[,2],input$topN)
#Expansion of the SenderList
SenderMailList <- filter(dataset(),Sender %in% centralityData)
#1st hop
FirstReceiver <- unique(SenderMailList$Receiver)
SenderVec <- centralityData
FinalSenderList <- append(SenderVec,FirstReceiver)
#2nd hop
SenderMailList <- filter(dataset(),Sender %in% FinalSenderList)
SenderMailList <- unique(SenderMailList)
#Appending a column with value 1( to be used in force n/w value option)
SenderMailList$One <- rep(1,nrow(SenderMailList))
#ordering the department data
deptData <- dataset2()
deptData <- deptData[with(deptData, order(Code)),]
#Plotting the Force N/w
forceNetwork(Links = head(SenderMailList,250) , Nodes = deptData , Source = "Sender",
Target = "Receiver",Value = "One",NodeID = "Code",
Group = "Department", opacity = 1, arrows = TRUE,
legend = TRUE, linkColour = "#666", zoom = TRUE, linkDistance = 80, height=90,width=150,fontSize = 30,
bounded = FALSE, opacityNoHover = 0,
clickAction = NULL )
})
###########
#BetweenNess
output$Betweeness <- renderDataTable({
calbetween()
})
#Plotting Betweenness
output$betweennessPlot <-renderForceNetwork({
betweennessData <- head(calbetween()[,2],input$topN)
#Expansion of the SenderList
SenderMailList <- filter(dataset(),Sender %in% betweennessData)
#1st hop
FirstReceiver <- unique(SenderMailList$Receiver)
SenderVec <- betweennessData
FinalSenderList <- append(SenderVec,FirstReceiver)
#2nd hop
SenderMailList <- filter(dataset(),Sender %in% FinalSenderList)
SenderMailList <- unique(SenderMailList)
#simpleNetwork(head(SenderMailList,50))
#Appending a column with value 1( to be used in force n/w value option)
SenderMailList$One <- rep(1,nrow(SenderMailList))
#ordering the department data
deptData <- dataset2()
deptData <- deptData[with(deptData, order(Code)),]
#Plotting the Force N/w
forceNetwork(Links =head(SenderMailList,250) , Nodes = deptData , Source = "Sender",
Target = "Receiver",Value = "One",NodeID = "Code",
Group = "Department", opacity = 1, arrows = TRUE,
legend = TRUE, linkColour = "#666", zoom = TRUE, linkDistance = 80, height=90,width=150,fontSize = 30,
bounded = FALSE, opacityNoHover = 0,
clickAction = NULL )
})
###########
#Calculating In degree centrality
output$InCentrality <- renderDataTable({
calindegree()
})
############
#Plotting Betweenness
output$inPlot <-renderForceNetwork({
inDegreeData <- head(calindegree()[,2],10)
#Expansion of the SenderList
SenderMailList <- filter(dataset(),Sender %in% inDegreeData)
#1st hop
FirstReceiver <- unique(SenderMailList$Receiver)
SenderVec <- inDegreeData
FinalSenderList <- append(SenderVec,FirstReceiver)
#2nd hop
SenderMailList <- filter(dataset(),Sender %in% FinalSenderList)
SenderMailList <- unique(SenderMailList)
#simpleNetwork(head(SenderMailList,50))
#Appending a column with value 1( to be used in force n/w value option)
SenderMailList$One <- rep(1,nrow(SenderMailList))
#ordering the department data
deptData <- dataset2()
deptData <- deptData[with(deptData, order(Code)),]
#Plotting the Force N/w
forceNetwork(Links = head(SenderMailList,250) , Nodes = deptData , Source = "Sender",
Target = "Receiver",Value = "One",NodeID = "Code",
Group = "Department", opacity = 1, arrows = TRUE,
legend = TRUE, linkColour = "#666", zoom = TRUE, linkDistance = 80, height=90,width=150,fontSize = 30,
bounded = FALSE, opacityNoHover = 0,
clickAction = NULL )
})
#Department Interaction
output$department <- renderDataTable({
mailData <- dataset()
deptData <- dataset2()
#Appending the sender department
names(deptData)[1] <- names(mailData)[1]
SenderDeptData <- left_join(mailData, deptData , by = c(names(mailData)[1],names(deptData)[1]))
#Appending the receiver department
names(SenderDeptData)[2] -> names(deptData)[1]
EmpDeptData <- left_join(SenderDeptData, deptData , by = c(names(SenderDeptData)[2],names(deptData)[1]))
names(EmpDeptData) <- c("Sender","Receiver","Sender_Department","Receiver_Department")
DeptInteraction <- EmpDeptData[,3:4]
DeptInteraction <- count(DeptInteraction,c("Sender_Department", "Receiver_Department"))
#mail_department <- mail_department[!(mail_department$Sender_Department==mail_department$Receiver_Department),]
#mail_department <- head(mail_department[order(mail_department$freq, decreasing = T),])
DeptInteraction
})
############
#Department Interaction Plot
output$deptplot <-renderPlot({
mailData <- dataset()
deptData <- dataset2()
#Appending the sender department
names(deptData)[1] <- names(mailData)[1]
SenderDeptData <- left_join(mailData, deptData , by = c(names(mailData)[1],names(deptData)[1]))
#Appending the receiver department
names(SenderDeptData)[2] -> names(deptData)[1]
EmpDeptData <- left_join(SenderDeptData, deptData , by = c(names(mailData)[2],names(deptData)[1]))
names(EmpDeptData) <- c("Sender","Receiver","SenderDepartment","ReceiverDepartment")
#DeptWiseInteraction
DeptInteraction <- unique(EmpDeptData[,3:4] %>% group_by(SenderDepartment,ReceiverDepartment))
DeptInteraction <- as.matrix(DeptInteraction)
graph <- graph_from_data_frame(head(DeptInteraction,50) ,directed = T)
plot(graph)
# plot.igraph(graph,edge.color="black",vertex.color="white",
# vertex.label="SenderDepartment",vertex.size=20,vertex.label.cex=3,
# vertex.label.color="black",edge.width=3,edge.arrow.size=1.2,edge.arrow.width=1.2)
})
###############
})