Project Status: Active – The project has reached a stable, usable state and is being actively developed. license Lifecycle: maturing codecov Build Status

knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.path = "man/figures/README-",
  out.width = "100%"
)

gephi

This is a simple package to export files into a csv format that gephi can understand. This package does not interface with the open source network vizualisation software gephi but it writes and reads in the same csv format as gephi.

I've found the need to convert tidygraph/igraph objects into a node and edge csv, to visualize in gephi quite often. This task should be trivial, but gephi is a bit particular and wants specific column names.

What does the package do?

Writes igraph files to csv format that gephi likes. Really? Gephi reads csv files just fine! Sure but it wants the columns in a particular order and named Source, Target etc. Let's not do this by hand everytime: AUTOMATE THE BORING STUFF! HACK THE PLANET!

Installation

Install this developmental version with:

# install.packages("devtools")
devtools::install_github("RMHogervorst/gephi")

Example

Writing out an igraph file to csv:

library(gephi) # includes the graphexample file
library(igraph)
V(graphexample)
E(graphexample)
gephi_write_edges(graphexample, "edges.csv")

Technically an tidygraph object is also an igraph object so the writing will work the same.

library(gephi)
library(tidygraph)

(tidy_graphexample <- tidygraph::as_tbl_graph(graphexample)) # Just to show where this function comes from

More specifically if you want to modify your graph and visualize a subset in gephi, here is a tidygraph worked example where I select only the edges that are blue, add a new edge property and write the resulting graph to csv:

tidy_graphexample %>%  # but the igraph object works just as well
    activate(nodes) %>% 
    filter(color == "blue") %>% 
    activate(edges) %>% 
    mutate(dongle = "dingle") %>% 
    gephi_write_edges("edges_subset.csv") %>% 
    print()

But is is also possible to write a set of edges when there is no graph object, just a dataframe.

a_nice_df <- data.frame(
    start = c(1,2,3,4,5,6,7),
    finish = c(2,4,4,7,2,1,3),
    weight = c(1,1,1,2,6,1,1)
)
print(a_nice_df) 
gephi_write_edges_from_df(a_nice_df, path = "edges2.csv")

Test coverage statistics

covr::package_coverage(type = "tests")

cleaning up after ourselves for this demo

file.remove("edges.csv")
file.remove("edges2.csv")
file.remove("edges_subset.csv")

Links



RMHogervorst/gephi documentation built on July 9, 2022, 6:50 a.m.