-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathserver.R
224 lines (190 loc) · 6.45 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
library(shiny)
library(dplyr)
library(ggplot2)
library(shinydashboard)
shinyServer(function(input, output, session) {
node_list <- reactive({
switch(
input$graph_set,
"goltzius" = goltzius_node_list,
"les_mis" = lm_node_list,
"karate" = karate_node_list,
"polbooks" = polbooks_node_list,
"copperfield" = copperfield_node_list,
"football" = football_node_list
)
})
edge_list <- reactive({
switch(
input$graph_set,
"goltzius" = goltzius_edge_list,
"les_mis" = lm_edge_list,
"karate" = karate_edge_list,
"polbooks" = polbooks_edge_list,
"copperfield" = copperfield_edge_list,
"football" = football_edge_list
)
})
output$attribution <- renderUI({
switch(
input$graph_set,
"goltzius" = includeMarkdown("data/citations/goltzius.md"),
"les_mis" = includeMarkdown("data/citations/les_mis.md"),
"karate" = includeMarkdown("data/citations/karate.md"),
"polbooks" = includeMarkdown("data/citations/polbooks.md"),
"copperfield" = includeMarkdown("data/citations/copperfield.md"),
"football" = includeMarkdown("data/citations/football.md")
)
})
# Generate a selection menu for ordering choices
output$ordering_choices <- renderUI({
base <- c(
"name",
"community"
)
dataset_names <- names(node_list())
var_choices <- dataset_names[grep("comm", dataset_names, invert = TRUE)] %>% union(base)
return(selectInput(
"arr_var",
"Arrange by",
choices = var_choices,
selected = "name"
))
})
# Generate a selection menu for community detection choices
output$comm_choices <- renderUI({
dataset_names <- names(node_list())
comm_choices <- dataset_names[grep("comm", dataset_names)]
return(selectInput(
"comm_var",
"Community Algorithm",
choices = comm_choices,
selected = "walktrap_comm"
))
})
weighted <- reactive({
return("weight" %in% names(edge_list()))
})
output$weighted <- reactive({weighted()})
outputOptions(output, 'weighted', suspendWhenHidden = FALSE)
# List non-calculated node attributes
annotate_vars <- reactive({
dataset_names <- names(node_list())
base <- c(
"name",
"degree",
"closeness",
"betweenness",
"eigen",
"walktrap_comm",
"edge_comm",
"optimal_comm",
"spinglass_comm",
"fastgreedy_comm",
"multilevel_comm"
)
return(setdiff(dataset_names, base))
})
annotatable <- reactive({
return(input$arr_var %in% annotate_vars())
})
output$annotate_vars <- reactive({annotatable()})
outputOptions(output, "annotate_vars", suspendWhenHidden = FALSE)
# Returns a character vector of the vertices ordered based on given variables
ordering <- reactive({
if(input$arr_var == "community") {
return((node_list() %>% arrange_(input$comm_var))$name)
} else {
return((node_list() %>% arrange_(input$arr_var))$name)
}
})
# Determine a community for each edge. If two nodes belong to the
# same community, label the edge with that community. If not,
# the edge community value is 'NA'
coloring <- reactive({
colored_edges <- edge_list() %>%
inner_join(node_list() %>% select_("name", "community" = input$comm_var), by = c("from" = "name")) %>%
inner_join(node_list() %>% select_("name", "community" = input$comm_var), by = c("to" = "name")) %>%
mutate(group = ifelse(community.x == community.y, community.x, NA) %>% factor())
return(colored_edges)
})
# Sort the edge list based on the given arrangement variable
plot_data <- reactive({
name_order <- ordering()
sorted_data <- coloring() %>% mutate(
to = factor(to, levels = name_order),
from = factor(from, levels = name_order))
return(sorted_data)
})
output$adj_plot <- renderPlot({
if(weighted() & input$alpha_weight) {
p <- ggplot(plot_data(), aes(x = from, y = to, fill = group, alpha = weight))
} else {
p <- ggplot(plot_data(), aes(x = from, y = to, fill = group))
}
p <- p + geom_raster() +
theme_bw() +
# Because we need the x and y axis to display every node,
# not just the nodes that have connections to each other,
# make sure that ggplot does not drop unused factor levels
scale_x_discrete(drop = FALSE) +
scale_y_discrete(drop = FALSE) +
theme(
# Rotate the x-axis lables so they are legible
axis.text.x = element_text(angle = 270, hjust = 0, size = 12),
axis.text.y = element_text(size = 12),
# Force the plot into a square aspect ratio
aspect.ratio = 1,
# Hide the legend (optional)
legend.position = "bottom")
# Annotate the plot based on preexisting node attributes
if(annotatable() & input$ann_var) {
# Determine the "first" and "last" members of a node group
ordered_anns <- node_list() %>%
group_by_(input$arr_var) %>%
summarize(min = first(name), max = last(name)) %>%
filter(min != max)
ann_groups <- ordered_anns[[input$arr_var]]
# For each node grouping, add an annotation layer
for(val in ann_groups[!is.na(ann_groups)]) {
# Retrieve the min and max value for the given group value
ann_min <- ordered_anns[ordered_anns[, input$arr_var] == val, ][["min"]]
ann_max <- ordered_anns[ordered_anns[, input$arr_var] == val, ][["max"]]
p <- p + annotate(
"rect",
xmin = ann_min,
xmax = ann_max,
ymin = ann_min,
ymax = ann_max,
alpha = .1) +
annotate(
"text",
label = val,
x = ann_min,
y = ann_max,
hjust = 0
)
}
}
return(p)
})
comm_membership <- reactive({
membership_list <- node_list() %>%
select_("name", "community" = input$comm_var)
return(membership_list)
})
# Render an HTML list of community memberships beneath the display
output$membership_list <- renderUI({
# Get a table of community memberships based on the selected community
# detection method
members <- comm_membership()
comms <- unique(members$community)
# Create and populate a list of HTML elements
member_html <- list()
for(i in comms) {
group_membs <- members$name[members$community == i]
member_html[[i]] <- list(box(title = paste("Group", i), width = 3, status = "info", p(group_membs)))
}
return(member_html)
})
})