rshinygephivisnetwork

Export a VisNetwork plot to Gephi


My R/Shiny application generates network plots using VisNetwork. I need to export them to Gephi. What is the best way? Node colors, line width, background color (and aesthetics in general) are not important. I want to export the data. In Gephi I should get a similar plot.

According to Gephi's documentation optimal formats are GEFX (edge weight, hierarchy and XML structure) or CSV (edge list / matrix). However, VisNetwork does not export these formats. Gephi can export a JSON file which can be opened with VisNetwork, but the contrary isn't possible. VisNetwork has visNetwork(gephi = 'network.json') but it does not export a JSON file, only import. visExport() only allows to export an image (png/jpg/pdf).

VisNetwork plot:

nodes <- data.frame(id = 1:10, label = paste("Label", 1:10),
   group = sample(c("A", "B"), 10, replace = TRUE))

edges <- data.frame(from = c(2,5,10), to = c(1,2,10))
network <- visNetwork(nodes, edges, height = "400px", width = "100%")

visNetwork(gephi = 'network.json') #does not work

I will probably have to code a function that writes graph data into a text file. What is the best way to do so? Should I privilege a specific format?


Solution

  • I know it is a bit late but I will try to help anyway. Gephi reads interactions from CSV files quite well. So:

    R

    You example is almost ok, just a litte change in the edges columns name:

    nodes <- data.frame(id = 1:10, label = paste("Label", 1:10), 
       group = sample(c("A", "B"), 10, replace = TRUE))
    
    edges <- data.frame(from = c(2,5,10), to = c(1,2,10))
    colnames(edges)<-c('source', 'target')
    

    Then you should export as CSV:

    write.csv(edges,'edges.csv')
    write.csv(nodes,'nodes.csv')
    

    Gephi

    Then, we go to gephi and:

    File -> import spreadsheet

    Please note:

    1. You will have to import edges and nodes one file at a time
    2. You will have to import both csv files to the same "workspace", or you will have two different graphs (empty edges and empty nodes)