Learn R Programming

cyjShiny

cyjShiny is a Shiny widget based on htmlWidgets for network visualization using cytoscape.js.

Installation

From CRAN (Stable Version)

Users should start with CRAN as it is the most stable version:

install.packages("cyjShiny") 

Install from GitHub (Development Version)

library(remotes)
remotes::install_github(repo="cytoscape/cyjShiny", ref="master", build_vignette=TRUE)

Compile cytoscape.js (Javascript Development)

Instructions for compiling cytoscape.js for use with htmlWidgets. NOTE: This should only be used by those actively modifying cytoscape.js.

Quick Start (First cyjShiny App)

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)

Styling

Many of the visual properties of a network can be stylized.

  • Styling Documentation
  • Example Styling (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"
  }}
]
  • Styling Usage with Quick Start Example:

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:

cyjShiny(graph_json, layoutName="cola", styleFile="style.js")

Demos

  • Try basicDemo Demo on shinyapps.io
  • basicDemo Code
  • Other Demos:
    • quickStartDemo: Demo used in README; Code
    • rmarkdownDemo: Demo for cyjShiny use in R Markdown files; Code
    • dataFrameGraphWithPresetNodePositions: Use preset layout to set node positions; Code
    • fromCytoscapeDesktop: Load and visualize networks generated in Cytoscape Desktop; Code
    • saveAndRestoreLayout: Within a session, save node positions and restore positions for nodes moved after positions saved; Code
    • withDT: Include at DT table of node values along with the graph visualization; Code

Copy Link

Version

Install

install.packages('cyjShiny')

Monthly Downloads

244

Version

1.0.42

License

MIT + file LICENSE

Maintainer

Augustin Luna

Last Published

March 28th, 2023

Functions in cyjShiny (1.0.42)

readAndStandardizeJSONStyleFile

Read in a JSON file, extract the selector elements, return JSON
showAll

Show all all selected nodes and their edges are hidden
removeGraph

Remove the current graph
savePNGtoFile

Save a png rendering of the current network view to the specified filename
selectFirstNeighbors

Select first neighbors of the currently selected nodes
setNodeAttributes

Assign the supplied node attribute values to the graph structure contained in the browser.
invertSelection

Invert selection all selected nodes and their edges are hidden
hideSelection

Hide selection all selected nodes and their edges are hidden
setNodePositions

Set node positions from the supplied data.frame
renderCyjShiny

More shiny plumbing - a cyjShiny wrapper for htmlwidget standard rendering operation
selectNodes

Select Nodes
setEdgeAttributes

Assign the supplied edge attribute values to the graph structure contained in the browser.
doLayout

Layout the current graph using the specified strategy.
readAndStandardizeJSONNetworkFile

Read in a JSON network file, identify (or add) elements field return JSON
fitSelected

Set zoom and center of the graph display so that the currently selected nodes fill the display
getNodePositions

Get node positions
fit

Set zoom and center of the graph display so that graph fills the display.
graphNELtoJSON

Convert R graphNEL object to cytoscape.js JSON.
addGraphFromDataFrame

Add graph from data.frame
clearSelection

Clear selection all node and edge selections removed
loadNetworkFromJSONFile

Load a standard cytoscape.js JSON network file
cyjShiny

cyjShiny cyjShiny
getSelectedNodes

Get Selected Nodes
addGraphFromJsonFile

Add graph from JSON file
loadStyleFile

Load a standard cytoscape.js style file
cyjShinyOutput

Standard shiny ui rendering construct
dataFramesToJSON

Create a cytoscape.js JSON graph from one or two data.frames.