-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.R
More file actions
276 lines (237 loc) · 10.3 KB
/
Copy pathapp.R
File metadata and controls
276 lines (237 loc) · 10.3 KB
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
#' @importFrom ariadne ariadne drawPath weavePath weaveComplex
#' @importFrom data.table fwrite
server <- function(input, output) {
# Import ariadne graph
graph <- ariadne() |>
as_undirected(mode = "each")
# Set node and edge ids
vertex_attr(graph, "id") <- V(graph)$name
edge_attr(graph, "id") <- seq_along(E(graph))
# Retrieve node and edge data
node_df <- as_data_frame(graph, what = "vertices")
edge_df <- as_data_frame(graph, what = "edges")
# Create default network
output$network <- renderVisNetwork({
# Define default network
visIgraph(graph, randomSeed = 123) |>
visNodes(color = "darkorange") |>
visEdges(color = list(color = "lightgrey", highlight = "red")) |>
visInteraction(
dragNodes = FALSE,
multiselect = TRUE,
selectConnectedEdges = FALSE
) |>
visEvents(
select = "function(x) {
Shiny.onInputChange('net_data', x);
;}"
)
})
# Select resources
observe({
# Based on whether resources are defined
if( is.null(input$resource) ){
# Select all nodes and edges
node_df$hidden <- FALSE
edge_df$hidden <- FALSE
}else{
# Find edges included in selected resources
edge_selected <- edge_df$source %in% input$resource
# Find nodes included in selected resources
nodes <- unique(
c(edge_df$from[edge_selected], edge_df$to[edge_selected])
)
# Hide edges and nodes absent in selected resources
edge_df$hidden <- !edge_selected
node_df$hidden <- !node_df$id %in% nodes
}
# Update network
visNetworkProxy("network") |>
visUpdateNodes(nodes = node_df) |>
visUpdateEdges(edges = edge_df)
})
# Reset parameters when one or less nodes are selected
observe({
# Check observe requirements
nodes <- unlist(input$net_data$nodes)
req(length(nodes) < 2L)
# Reset edge labels
edge_df$label <- " "
# If one node is selected
if( !is.null(nodes) ){
# Reset network
visNetworkProxy("network") |>
visUpdateEdges(edges = edge_df) |>
visSetSelection(nodesId = nodes, highlightEdges = FALSE)
}
# Reset observers to default
updateTextInput(inputId = "by", value = NA)
updateNumericInput(inputId = "k", value = 1)
updateSelectInput(inputId = "include", selected = NA)
updateSelectInput(inputId = "exclude", selected = NA)
})
# Draw pathway when two or more nodes are selected
observe({
# Check observe requirements
nodes <- unlist(input$net_data$nodes)
req(length(nodes) > 1L)
# Define pathway formula
path_by <- c(nodes[1], nodes[length(nodes)]) |>
paste(collapse = "~") |>
as.formula()
# Add intermediate nodes to include argument
include <- if(length(nodes) > 2L) nodes[2:(length(nodes) - 1)] else NULL
# Update observers based on selected pathway
updateTextInput(inputId = "by", value = deparse(path_by))
updateSelectInput(inputId = "include", selected = include)
# Draw selected pathway
path_df <- drawPath(
graph,
by = path_by,
k = input$k,
include = input$include,
exclude = input$exclude,
res.name = input$resource
)
# Get id of edges in the selected pathway
edges <- get_edge_ids(graph, path_df)
# Find selected edges and nodes
edge_selected <- edge_df$id %in% edges
node_selected <- node_df$id %in% union(path_df$from, path_df$to)
# Label selected edges with resource name
edge_df$label <- ifelse(edge_selected, edge_df$source, " ")
# If prune is on, hide unselected nodes and edges
node_df$hidden <- if(input$prune) !node_selected else FALSE
edge_df$hidden <- if(input$prune) !edge_selected else FALSE
# Update network
visNetworkProxy("network") |>
visUpdateNodes(nodes = node_df) |>
visUpdateEdges(edges = edge_df) |>
visSetSelection(
nodesId = nodes,
edgesId = edges,
highlightEdges = FALSE
)
# Download map table
output$draw <- downloadHandler(
filename = function(){
# Make file name from formula
path_name <- path_by |>
all.vars() |>
paste(collapse = "2")
# Complete file name
paste0(path_name, "-map-", Sys.Date(), ".tsv")
},
content = function(file) fwrite(path_df, file, sep = "\t")
)
})
# If focus is turned on
observeEvent(input$focus, {
# Fit network
visNetworkProxy("network") |>
visFit(nodes = unlist(input$net_data$nodes))
})
# Weave and download linkmap
output$weave <- downloadHandler(
filename = function(){
# Make file name from formula
path_name <- input$by |>
as.formula() |>
all.vars() |>
paste(collapse = "2")
# Complete file name
paste0(path_name, "-links-", Sys.Date(), ".tsv")
},
content = function(file){
# Select function by weave type
FUN <- switch(
input$weave_type, simple = weavePath, complex = weaveComplex
)
# Weave pathway
x2y <- FUN(
graph,
by = as.formula(input$by),
k = input$k,
include = input$include,
exclude = input$exclude,
res.name = input$resource,
init = input$text_init,
prune = input$weave_prune,
use.names = input$names,
threshold = input$threshold,
batch.size = input$batch_size,
factor = input$factor#,
#buffer, maxattempt
)
# Write linkmap to file
fwrite(x2y, file, sep = "\t")
}
)
}
#' @importFrom htmltools br div
#' @importFrom bslib page_sidebar bs_theme sidebar navset_tab nav_panel accordion accordion_panel
ui <- function(){
graph <- ariadne()
page_sidebar(
fillable = FALSE,
theme = bs_theme(bootswatch = "united"),
sidebar = sidebar(navset_tab(
nav_panel("Explore", br(),
textInput("by", "Path:", placeholder = "from ~ to"),
numericInput("k", "k:", value = 1, min = 1),
selectInput("include", "Include:", choices = V(graph)$name,
selected = NULL, multiple = TRUE),
selectInput("exclude", "Exclude:", choices = V(graph)$name,
selected = NULL, multiple = TRUE),
selectInput("resource", "Resource:",
choices = unique(E(graph)$source), selected = NULL,
multiple = TRUE),
checkboxInput("focus", "Focus"),
checkboxInput("prune", "Prune"),
accordion(open = FALSE, height = "80%",
accordion_panel("Advanced",
numericInput("buffer", "Buffer factor:", value = 2,
min = 1, step = 1),
numericInput("max_attempt", "Max attempts:", value = 5,
min = 1, step = 1))),
div(style = "margin-top: +20px"),
downloadButton(outputId = "draw", label = "Draw",
class = "btn-warning", icon = icon("pencil"),
style = "float: right;")),
nav_panel("Weave", br(),
tags$style(HTML("
.shiny-options-group .radio-inline {margin-right: 1rem;}
")),
radioButtons("weave_type", "Type:",
choices = c("simple", "complex"), inline = TRUE),
radioButtons("init_type", "Initial values as:",
choices = c("text", "file"), inline = TRUE),
conditionalPanel(condition = "input.init_type == 'text'",
selectizeInput("init_text", "Initial values:",
choices = NULL, multiple = TRUE,
options = list(create = TRUE))),
conditionalPanel(condition = "input.init_type == 'file'",
fileInput("init_file", "Initial values:",
accept = c("csv", "tsv"))),
checkboxInput("names", "Add names", value = TRUE),
accordion(open = FALSE, height = "80%",
accordion_panel("Advanced",
checkboxInput("weave_prune", "Prune", value = TRUE),
numericInput("batch_size", "Batch size:",
value = NULL, min = 1),
numericInput("factor", "Jobs per unit:", value = 3,
min = 1, step = 1))),
div(style = "margin-top: +20px"),
conditionalPanel(condition = "input.weave_type == 'complex'",
sliderInput("threshold", "Threshold:", min = 0, max = 1,
value = 0, step = 0.01, ticks = FALSE)),
downloadButton(outputId = "weave", label = "Weave",
class = "btn-warning", icon = icon("pencil"),
style = "float: right;")))),
visNetworkOutput("network", height = "100vh", width = "100vw")
)
}
#' @export
shinePath <- function(){
shinyApp(ui = ui(), server = server)
}