Skip to content

Instantly share code, notes, and snippets.

@ngopal
Created June 18, 2016 20:18
Show Gist options
  • Save ngopal/b7ada612bf2612fa2f00b86d05a34a2b to your computer and use it in GitHub Desktop.
Save ngopal/b7ada612bf2612fa2f00b86d05a34a2b to your computer and use it in GitHub Desktop.
Convert iGraph to CytoscapeJS elements
function toCytojs(g) {
s = c()
s = append(s,"[")
for (i in 1:length(V(g))) {
s = append(s, paste("{ data: { id: \'",i,"\' } },", sep=''))
}
temp <- get.edgelist(g)
for (i in 1:length(E(g))) {
if (i == length(E(g))) {
s = append(s, paste("{ data: { id: \'",i+length(V(g)),"\', source: \'",temp[i,1],"\', target: \'",temp[i,2],"\' } }", sep=''))
}
else {
s = append(s, paste("{ data: { id: \'",i+length(V(g)),"\', source: \'",temp[i,1],"\', target: \'",temp[i,2],"\' } },", sep=''))
}
}
s = append(s, "]")
return( paste(s, collapse="") )
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment