Skip to contents

Editing and interact with network data is not always easy. This package envelope a bundle of shiny modules for editing and interact with small amount of graph data, and let you download and load them back in as a excel spreadsheet.

This package is intended for R developers who have already familiar with basic workflow shiny and shiny module. If you are not already familiar with shiny module , have a skim at Hadley Wickham’s Mastering Shiny.

There are three pairs pre-written shiny module in this R package that will help you build interactive graph-based shiny application quickly. They are:

  • mod_visNetModification_<ui/server> for visualize, main graph and editing.
  • mod_visNetInteraction_<ui/server> for interactively select data
  • mod_fileUploader_<ui/server> for ingesting node/edge list
  • In addition mod_visNet_server, a serverside short-cut for use mod_visNetModification_server and mod_visNetInteraction_server at the same time.

This development heavy depends on visNetwork package and will works most responsive with graph of below 500 nodes.

Installation

You can install the development version of networkUtils from GitHub with:

remotes::install_github("FzliangFrank/networkUtils")

Introduction

Network/Graph data are structured usually in edge list or adjacency matrix. This data formats are not easy for human to interact with or editing.

The modules written in this package let human interactively search and editing network based data interactively. It extend interactivity from popular javascript library {vis.js} and data-storm’s {visNetwork} and make it easily accessible for R-user.

There we have the networkUtil packages. A make

selecting node or edge based on attributes

using mod_visInteractive_ui() and mod_visInteractive_server

editing graph structure

using mod_visModification_ui() and mod_visModification_server()

file upload and download server

using mod_fileUploader_ui() and mod_fileUploader_server().

Geting Started

You need two shiny module made it possible:

  • mod_visNetModification_server(id) (ui) in charge of the logic to editing networkgraph. This function return reactive graph.
  • mod_visNetInteraction_server(id) (ui) in charge of all the dynamic UI and selection by node.

Make sure the id matches. You can build your own shinyApp using these two module.

library(shiny)
library(igraph)
ui <- fluidPage(
  column(4,
         # must-have ui for interactive control bar
         mod_visNetInteraction_ui("id")
         ),
  column(8, 
         # must-have ui for main graph
         mod_visNetModification_ui("id")
         )  
)

server <- function(input, output, session) {
  myGraph = reactive({
    # fake a graph 
    n = 20
    b = 3
    g <- igraph::make_tree(n, b, mode = "out")
    # fake some attributes
    nV <- length(V(g))
    nE <- length(E(g))
    V(g)$label <- sample(letters, nV, replace = T) # fix this latter NAME needs to not identical
    V(g)$attr1_int <- sample(seq(10), nV, replace = T)
    V(g)$attr2_letter <- sample(LETTERS, nV, replace = T)
    V(g)$attr3_numb <- runif(nV) * 100
    sp_df = data.frame(x = runif(nV), y= runif(nV)) |> sf::st_as_sf(coords=c('x', 'y'))
    V(g)$attr4_geom <- sp_df$geom
    E(g)$attr1 <- runif(nE) * 100
  })
  # hello world
  mod_visNet_server("id", 
                     myGraph,
                     hard_delete = F
                     ) # Output is reactive value
}
shinyApp(ui, server)

Note always pass igraph as reacitve expression. The output of mod_visNetModification_server is always a list of two:

  • $Current for the graph you are interacting with now
  • $Main for graph you are happy with (one after you press save)

This is a basic version control.

Customise how you want editing graph.

visNetwork_graphChange generates a massive list of command as what has been changed.

There is an function to transfrom command generated by visNetwork api into a modified graph. If you have a database set up you can use this function to export changes graph back to database.

  • modify_graph_i

For example

list_of_command = list(
  cmd = 'addNode',
  id = '31'
)
modify_graph_i(graph, list_of_command)

Detail of how that list should work. Go visit visNetwork documentation.

This function currently only accept one command at a time.

Siny App Build Using this Module

Simple Network Util App is avaiable on shiny.io