cyjShiny is a Shiny widget based on htmlWidgets for network visualization using cytoscape.js.
install.packages("cyjShiny")
Demo files are files in inst/demos
folder of the project
code repository: https://github.com/cytoscape/cyjShiny
library(cyjShiny)
# NETWORK DATA ----
tbl_nodes <- data.frame(id=c("A", "B", "C"),
size=c(10, 20, 30),
stringsAsFactors=FALSE)
# Must have the interaction column
tbl_edges <- data.frame(source=c("A", "B", "C"),
target=c("B", "C", "A"),
interaction=c("inhibit", "stimulate", "inhibit"),
stringsAsFactors=FALSE)
graph_json <- toJSON(dataFramesToJSON(tbl_edges, tbl_nodes), auto_unbox=TRUE)
# graph_json is a string with JSON content that is input to cytoscape.js
print(graph_json)
cyjShiny(graph=graph_json, layoutName="cola")
Many of the visual properties of a network can be stylized.
data()
maps data dynamically to
specify a property value from the input data.frame):[
{"selector":"node", "css": {
"border-width": "2px",
"width": "data(size)",
"height": "data(size)",
"content": "data(id)"
}},
{"selector": "edge[interaction='stimulate']", "css": {
"line-color": "green"
}},
{"selector": "edge[interaction='inhibit']", "css": {
"line-color": "red"
}}
]
Save the example styling to a file style.js
in the
current working directory and replace cyjShiny()
in the
Quick Start example as shown below:
Cytoscape.js includes many layouts by default, including: cola, cose, circle, concentric, grid, breadthfirst, random, fcose, spread, preset
yeast_galactose_style_file <- system.file(file.path("demos", "rmarkdownDemo", "yeastGalactoseStyle.js"), package="cyjShiny")
yeast_galactose_graph <- readLines(system.file(file.path("demos", "rmarkdownDemo", "yeastGalactose.cyjs"), package="cyjShiny"))
cyjShiny(yeast_galactose_graph, layoutName="fcose", styleFile=yeast_galactose_style_file)
The preset
layout can be used to retain a layout
Networks from the Cytoscape Desktop can also be visualized within cyjShiny. Users can export the network for use with cytoscape.js (cyjShiny-compatible) format in this way:
File -> Export -> Network to File -> Export File Format: "Cytoscape.js JSON (*.js)"
Any layouts generated in Cytoscape Desktop can be retained in
cyjShiny by using the preset
layout as shown in the
example.
The following code create a minimal R Shiny application with network visualization using cyjShiny and cytoscape.js.
> library(shiny)
> library(cyjShiny)
> library(graph)
> library(jsonlite)
>
> # NETWORK DATA ----
> tbl_nodes <- data.frame(id=c("A", "B", "C"),
+ size=c(10, 20, 30),
+ stringsAsFactors=FALSE)
>
> # Must have the interaction column
> tbl_edges <- data.frame(source=c("A", "B", "C"),
+ target=c("B", "C", "A"),
+ interaction=c("inhibit", "stimulate", "inhibit"),
+ stringsAsFactors=FALSE)
>
> graph_json <- toJSON(dataFramesToJSON(tbl_edges, tbl_nodes), auto_unbox=TRUE)
>
> # UI ----
> ui <- fluidPage(cyjShinyOutput('cyjShiny'))
>
> # SERVER ----
> server <- function(input, output, session) {
+ output$cyjShiny <- renderCyjShiny({
+ # Layouts (see js.cytoscape.org): cola, cose, circle, concentric, grid, breadthfirst, random, fcose, spread
+ cyjShiny(graph_json, layoutName="cola")
+ })
+ }
>
> # RUN ----
> shinyApp(ui=ui, server=server)