-
Notifications
You must be signed in to change notification settings - Fork 28
/
cyjShiny.R
524 lines (482 loc) · 16.5 KB
/
cyjShiny.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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
#' @importFrom jsonlite toJSON fromJSON
#' @importFrom htmlwidgets createWidget shinyWidgetOutput shinyRenderWidget
#' @importFrom base64enc base64decode
#' @import shiny
#'
#' @title cyjShiny
#------------------------------------------------------------------------------------------------------------------------
#' cyjShiny
#'
#' @description
#' This widget wraps cytoscape.js, a full-featured Javsscript network library for visualization and analysis.
#'
#' @aliases cyjShiny
#' @rdname cyjShiny
#'
#' @param graph an R graphNEL instance (igraph support coming soon).
#' @param layoutName character one of:"preset", "cose", "cola", "circle", "concentric", "breadthfirst", "grid", "random", "dagre", "cose-bilkent"
#' @param style_file, default NULL, can name a standard javascript cytoscape.js style file
#' @param width integer initial width of the widget.
#' @param height integer initial height of the widget.
#' @param elementId string the DOM id into which the widget is rendered, default NULL is best.
#'
#' @return a reference to an htmlwidget.
#'
#'
#' @examples
#' \dontrun{
#' output$cyjShiny <- renderCyjShiny(cyjShiny(graph))
#' }
#'
#' @export
cyjShiny <- function(graph, layoutName, style_file=NULL, width = NULL, height = NULL, elementId = NULL)
{
stopifnot(layoutName %in% c("preset",
"cose",
"cola",
"circle",
"concentric",
"breadthfirst",
"grid",
"random",
"dagre",
"cose-bilkent"))
if (is.null(style_file)) {
style = NULL
} else {
if(file.exists(style_file)){
jsonText <- toJSON(fromJSON(style_file)) # very strict parser, no unquoted field names
style <- list(json=jsonText)
} else {
warning(sprintf("cannot read style file: %s, continuing with default", style_file))
style = NULL
}
}
x <- list(graph=graph, layoutName=layoutName, style=style)
htmlwidgets::createWidget(
name = 'cyjShiny',
x,
width = width,
height = height,
package = 'cyjShiny',
elementId = elementId,
sizingPolicy = htmlwidgets::sizingPolicy(browser.fill=TRUE, )
# defaultWidth=500,
# defaultHeight=500,
# viewer.padding=0,
# viewer.suppress=FALSE,
# viewer.paneHeight=500,
# browser.fill=TRUE)
)
} # cyjShiny constructor
#------------------------------------------------------------------------------------------------------------------------
#' Standard shiny ui rendering construct
#'
#' @param outputId the name of the DOM element to create.
#' @param width integer optional initial width of the widget.
#' @param height integer optional initial height of the widget.
#'
#' @return a reference to an htmlwidget
#'
#' @export
#'
#' @examples
#' \dontrun{
#' mainPanel(cyjShinyOutput('cyjShiny'), width=10)
#' }
#'
#' @aliases cyjShinyOutput
#' @rdname cyjShinyOutput
#'
#' @export
cyjShinyOutput <- function(outputId, width = '100%', height = '400')
{
htmlwidgets::shinyWidgetOutput(outputId, 'cyjShiny', width, height, package = 'cyjShiny')
}
#------------------------------------------------------------------------------------------------------------------------
#' More shiny plumbing - a cyjShiny wrapper for htmlwidget standard rendering operation
#'
#' @param expr an expression that generates an HTML widget.
#' @param env environment in which to evaluate expr.
#' @param quoted logical specifies whether expr is quoted ("useuful if you want to save an expression in a variable").
#'
#' @return not sure
#'
#' @aliases renderCyjShiny
#' @rdname renderCyjShiny
#'
#' @export
renderCyjShiny <- function(expr, env = parent.frame(), quoted = FALSE)
{
if (!quoted){
expr <- substitute(expr)
} # force quoted
htmlwidgets::shinyRenderWidget(expr, cyjShinyOutput, env, quoted = TRUE)
}
#------------------------------------------------------------------------------------------------------------------------
#' load a standard cytoscape.js style file
#'
#' @param filename character string, either relative or absolute path.
#'
#' @return nothing
#'
#' @export
#'
#' @examples
#' \dontrun{
#' loadStyleFile(system.file(package="cyjShiny", "extdata", "yeastGalactoseStyle.js"))
#' }
#'
#' @aliases loadStyleFile
#' @rdname loadStyleFile
#'
#' @export
loadStyleFile <- function(filename)
{
if(!file.exists(filename)){
warning(sprintf("cannot read style file: %s", filename))
return();
}
jsonText <- toJSON(fromJSON(filename)) # very strict parser, no unquoted field names
#lines <- scan(filename, what=character(), strip.white=TRUE, quote="'")
#jsonText <- paste(lines, collapse=" ")
message <- list(json=jsonText)
session <- shiny::getDefaultReactiveDomain()
session$sendCustomMessage("loadStyle", message)
} # loadStyleFile
#------------------------------------------------------------------------------------------------------------------------
#' Set zoom and center of the graph display so that graph fills the display.
#'
#' @param session a Shiny server session object.
#' @param padding integer, default 50 pixels.
#'
#' @examples
#' \dontrun{
#' fit(session, 100)
#'}
#'
#' @aliases fit
#' @rdname fit
#'
#' @seealso\code{\link{fitSelected}}
#'
#' @export
fit <- function(session, padding=50)
{
session$sendCustomMessage("fit", list(padding=padding))
} # fitSelected
#------------------------------------------------------------------------------------------------------------------------
#' Set zoom and center of the graph display so that the currently selected nodes fill the display
#'
#' @param session a Shiny server session object.
#' @param padding integer, default 50 pixels.
#'
#' @examples
#' \dontrun{
#' fitSelected(session, 100)
#' }
#'
#' @aliases fitSelected
#' @rdname fitSelected
#'
#' @seealso\code{\link{fit}}
#'
#' @export
fitSelected <- function(session, padding=50)
{
session$sendCustomMessage("fitSelected", list(padding=padding))
} # fitSelected
#------------------------------------------------------------------------------------------------------------------------
#' getSelectedNodes
#'
#' \code{getSelectedNodes} get the selected nodes
#'
#' @rdname getSelectedNodes
#' @aliases getSelectedNodes
#'
#' @param session a Shiny server session object.
#'
#' @return a data.frame with (at least) an id column
#'
#' @export
#'
getSelectedNodes <- function(session)
{
session$sendCustomMessage("getSelectedNodes", message=list())
} # getSelectedNodes
#----------------------------------------------------------------------------------------------------
#' Assign the supplied node attribute values to the graph structure contained in the browser.
#'
#' @param session a Shiny Server session object.
#' @param attributeName character string, the attribute to update.
#' @param nodes a character vector the names of the nodes whose attributes are updated.
#' @param values a character, logical or numeric vector, the new values.
#'
#' @examples
#' \dontrun{
#' setNodeAttributes(session,
#' attributeName=attribute,
#' nodes=yeastGalactodeNodeIDs,
#' values=expression.vector)
#' }
#'
#' @aliases setNodeAttributes
#' @rdname setNodeAttributes
#'
#' @export
setNodeAttributes <- function(session, attributeName, nodes, values)
{
session$sendCustomMessage(type="setNodeAttributes",
message=list(attribute=attributeName,
nodes=nodes,
values=values))
} # setNodeAttributes
#------------------------------------------------------------------------------------------------------------------------
#' Assign the supplied edge attribute values to the graph structure contained in the browser.
#'
#' @param session a Shiny Server session object.
#' @param attributeName character string, the attribute to update.
#' @param sourceNodes a character vector, the names of the source nodes of the edges
#' @param targetNodes a character vector, the names of the target nodes of the edgees
#' @param interactions a character vector, further identifying the specific edge whose attributes are updated.
#' @param values a character, logical or numeric vector, the new values.
#'
#' @examples
#' \dontrun{
#' setEdgeAttributes(session,
#' attributeName="score",
#' sourceNodes=c("A", "B", "C"),
#' targetNodes=c("D", "E", "A"),
#' interactions=c("promotes", "promotes", "inhibits"),
#' values=new.scores)
#' }
#'
#' @aliases setEdgeAttributes
#' @rdname setEdgeAttributes
#'
#' @export
setEdgeAttributes <- function(session, attributeName, sourceNodes, targetNodes, interactions, values)
{
session$sendCustomMessage(type="setEdgeAttributes",
message=list(attributeName=attributeName,
sourceNodes=sourceNodes,
targetNodes=targetNodes,
interactions=interactions,
values=values))
} # setEdgeAttributes
#------------------------------------------------------------------------------------------------------------------------
#' Layout the current graph using the specified strategy.
#'
#' @param session a Shiny Server session object.
#' @param strategy a character string, one of cola, cose, circle, concentric, grid, breadthfirst, random, dagre, cose-bilkent.
#'
#' @examples
#' \dontrun{
#' doLayout(session, "cola")
#' }
#' @aliases doLayout
#' @rdname doLayout
#'
#' @export
doLayout <- function(session, strategy)
{
stopifnot(strategy %in% c("cola", "cose", "circle", "concentric", "grid", "breadthfirst", "random",
"dagre", "cose-bilkent"))
session$sendCustomMessage(type="doLayout", message=list(strategy=strategy))
} # doLayout
#------------------------------------------------------------------------------------------------------------------------
#' get node positions
#'
#' @param session a Shiny Server session object.
#'
#' @aliases getNodePositions
#' @rdname getNodePositions
#'
#' @export
getNodePositions <- function(session)
{
x <- session$sendCustomMessage(type="getNodePositions", message=list())
} # getNodePositions
#------------------------------------------------------------------------------------------------------------------------
#' set node positions from the supplied data.frame
#'
#' @param session a Shiny Server session object.
#' @param tbl.positions a data.frame with three columns: id, x, y
#'
#' @aliases setNodePositions
#' @rdname setNodePositions
#'
#' @export
setNodePositions <- function(session, tbl.positions)
{
stopifnot(colnames(tbl.positions) == c("id", "x", "y"))
tbl.json <- toJSON(tbl.positions) # force a json representation which is an array of {id,x,y{ objects
session$sendCustomMessage(type="setNodePositions", message=list(tbl=tbl.json))
} # setNodePositions
#------------------------------------------------------------------------------------------------------------------------
#' remove the current graph
#'
#' @param session a Shiny Server session object.
#'
#' @examples
#' \dontrun{
#' removeGraph(session)
#' }
#' @aliases removeGraph
#' @rdname removeGraph
#'
#' @export
removeGraph <- function(session)
{
message(sprintf("entering cyjShiny::removeGraph"))
session$sendCustomMessage(type="removeGraph", message=list())
} # removeGraph
#------------------------------------------------------------------------------------------------------------------------
#' addGraphFromDataFrame
#'
#' @param session a Shiny Server session object.
#' @param tbl.edges a data.frame with source, traget, interaction columns (and option other attributes)
#' @param tbl.nodes (optional; nodes can be deduced from tbl.edges) a data.frame with nodes and their attributes
#'
#' @examples
#' \dontrun{
#' addGraphFromDataFrame (session)
#' }
#'
#' @aliases addGraphFromDataFrame
#' @rdname addGraphFromDataFrame
#'
#' @export
addGraphFromDataFrame <- function(session, tbl.edges, tbl.nodes=NULL)
{
illegal.tbl <- ncol(tbl.edges) < 3
illegal.colnames <- !(all(colnames(tbl.edges)[1:3] == c("source", "target", "interaction")))
if(illegal.tbl | illegal.colnames){
msg <- sprintf("required colnames for tbl.edges: 'source' 'target' 'interaction'")
cat(msg)
return()
}
g.json <- dataFramesToJSON(tbl.edges, tbl.nodes)
session$sendCustomMessage(type="addGraph", message=list(graph=g.json))
} # addGraphFromDataFrame
#------------------------------------------------------------------------------------------------------------------------
#' addGraphFromJsonFile
#'
#' @param session a Shiny Server session object.
#' @param jsonFilename of a text file with JSON representation of a cytoscape.js graph
#'
#' @examples
#' \dontrun{
#' addGraphFromJsonFile (session)
#' }
#' @aliases addGraphFromJsonFile
#' @rdname addGraphFromJsonFile
#'
#' @export
#'
addGraphFromJsonFile <- function(session, jsonFilename)
{
g.json <- readLines(jsonFilename)
session$sendCustomMessage(type="addGraph", message=list(graph=g.json))
} # addGraphFromJSON
#------------------------------------------------------------------------------------------------------------------------
#' selectNodes
#'
#' @param session a Shiny Server session object.
#' @param nodeNames character, a list of node IDs
#'
#' @aliases selectNodes
#' @rdname selectNodes
#'
#' @export
#'
selectNodes <- function(session, nodeNames)
{
session$sendCustomMessage(type="selectNodes", message=list(nodeNames))
} # selectNodes
#------------------------------------------------------------------------------------------------------------------------
#' selectFirstNeighbors of the currently selected nodes
#'
#' @param session a Shiny Server session object.
#'
#' @aliases selectFirstNeighbors
#' @rdname selectFirstNeighbors
#'
#' @export
#'
selectFirstNeighbors <- function(session)
{
session$sendCustomMessage(type="sfn", message=list())
} # selectFirstNeighbors
#------------------------------------------------------------------------------------------------------------------------
#' clearSelection all node and edge selections removed
#'
#' @param session a Shiny Server session object.
#'
#' @aliases clearSelection
#' @rdname clearSelection
#'
#' @export
#'
clearSelection <- function(session)
{
session$sendCustomMessage(type="clearSelection", message=list())
} # clearSelection
#------------------------------------------------------------------------------------------------------------------------
#' invertSelection all selected nodes and their edges are hidden
#'
#' @param session a Shiny Server session object.
#'
#' @aliases invertSelection
#' @rdname invertSelection
#'
#' @export
#'
invertSelection <- function(session)
{
session$sendCustomMessage(type="invertSelection", message=list())
} # invertSelection
#------------------------------------------------------------------------------------------------------------------------
#' hideSelection all selected nodes and their edges are hidden
#'
#' @param session a Shiny Server session object.
#'
#' @aliases hideSelection
#' @rdname hideSelection
#'
#' @export
#'
hideSelection <- function(session)
{
session$sendCustomMessage(type="hideSelection", message=list())
} # hideSelection
#------------------------------------------------------------------------------------------------------------------------
#' showAll all selected nodes and their edges are hidden
#'
#' @param session a Shiny Server session object.
#'
#' @aliases showAll
#' @rdname showAll
#'
#' @export
#'
showAll <- function(session)
{
session$sendCustomMessage(type="showAll", message=list())
} # showAll
#------------------------------------------------------------------------------------------------------------------------
#' save a png rendering of the current network view to the specified filename
#'
#' @param session a Shiny Server session object.
#' @param filename a character string
#'
#' @aliases savePNGtoFile
#' @rdname savePNGtoFile
#'
#' @export
#'
savePNGtoFile <- function(session, filename)
{
session$sendCustomMessage(type="savePNGtoFile", message=list(filename))
} # clearSelection
#------------------------------------------------------------------------------------------------------------------------