David Schmudde / May 01 2019

Network Visualization in R

1. HTML Widgets

HTML Widgets support is currently experimental and can be enabled by running the following code.

options(nextjournal.display.htmlwidgetsEnabled=TRUE)

Install the visNetwork package from HTML Widgets.

install.packages("visNetwork")
library(visNetwork)

2. Data

Mount the BNShinyApp GitHub repo. Note that I'm using the BN_First_Version branch.

This is a Bash cell for the R runtime, aka Bash in R.

  • Working with Bash: use the + button to add a Bash cell. It should read Bash in R like the cell below. If not, use the ··· cell menu in the upper left: Change Runtime...Add New Runtime...
  • Add a repository: under the Runtime Menu, expand Mounts and then press Add mount. Select the repository's name under Source and the Destination field will be filled with a default.

Here is the GitHub repository on the command line:

ls BNShinyApp/*.csv

I could have just downloaded from the GitHub repo and re-uploaded, but this demonstrates more of what is possible with Nextjournal.

3. Display Graph

This is the exact same code from the GitHub repo (cut and paste), except for adding the BNShinyApp directory to the .csv files.

0.7s
R
nodes <- read.csv("BNShinyApp/NODES.csv", header=T, as.is=T)
links <- read.csv("BNShinyApp/EDGES.csv", header=T, as.is=T)

nodes$shape <- "dot"  
nodes$shadow <- TRUE # Nodes will drop shadow
nodes$title <- nodes$Variable # Text on click
nodes$label <- nodes$Variable
nodes$borderWidth <- 3 # Node border width

    
# We can set the color for several elements of the nodes:
# "background" changes the node color, "border" changes the frame color;
# "highlight" sets the color on click, "hover" sets the color on mouseover.
    
nodes$color.background <- c("slategrey", "tomato", "gold")[nodes$media.type]
nodes$color.border <- "black"
nodes$color.highlight.background <- "orange"
nodes$color.highlight.border <- "darkred"
    

# Below we change some of the visual properties of the edges:
    
#links$width <- 1+links$weight/8 # line width
links$color <- "gray"    # line color  
links$arrows <- "middle" # arrows: 'from', 'to', or 'middle'
links$smooth <- TRUE    # should the edges be curved?
links$shadow <- TRUE    # edge shadow
    
visNetwork(nodes, links) %>%
  visInteraction(dragView = TRUE,navigationButtons = TRUE)%>%
  visPhysics(solver = "forceAtlas2Based", 
             forceAtlas2Based = list(gravitationalConstant = -60))%>%
  #visIgraphLayout(,type = "square")%>%
  visLayout(randomSeed = 2069)%>%
  visEdges(width=2)%>% visNodes(font=list(size=20))%>%
  visOptions(selectedBy = "Type",highlightNearest = list(enabled = T, degree = 1, hover = T))