igraph / rigraph

igraph R package
https://r.igraph.org
554 stars 200 forks source link

R: exporter to D3.js #17

Open gaborcsardi opened 9 years ago

gaborcsardi commented 9 years ago

From @yrochat on March 4, 2014 11:30

I'd love to help implement an R function to do that http://coulmont.com/blog/2012/11/03/reseau-d3js/ I was wondering if it was worth it. And if you think it's the case, do you have advice, like: should I work on a C version (knowing that I haven't programmed in C for exactly 10 years) or work directly in R ?

Copied from original issue: igraph/igraph#595

gaborcsardi commented 9 years ago

Sounds great! R is fine, probably the jsonlite package is the best option.

gaborcsardi commented 9 years ago

From @yrochat on March 4, 2014 14:3

Cool, thanks! I've been thinking of a few things I'd like to do to help—and thank—in a few days/weeks, after writing my Ph.D. (It's overwhelmed by igraph figures.) I'll get back to you with some code :-)

gaborcsardi commented 9 years ago

Actually, probably you don't need any json package at all, at least not for writing the graph in json. That way we don't have to have additional dependencies.

gaborcsardi commented 9 years ago

From @yrochat on March 5, 2014 14:43

That's what I think, too. Like with the igraph to TikZ exporter : http://igraph.wikidot.com/r-recipes#toc2

gaborcsardi commented 9 years ago

From @yrochat on May 7, 2014 18:39

Found this package that exports to D3. I'm thinking either of converting an igraph object to pass it through that package, or bypassing it (still have to discover d3.js network format). http://cran.r-project.org/web/packages/d3Network/d3Network.pdf

gaborcsardi commented 9 years ago

From @mbojan on May 8, 2014 0:49

I was looking for such a thing too. Thanks for finding it! I think writing a wrapper around d3ForceNetwork should be rather straightforward with get.data.frameing the igraph object, passing the data frames to Nodes and Links arguments and setting appropriate column names in the other arguments to d3ForceNetwork, which BTW have rather "opinionated" defaults...

mbojan commented 9 years ago

I have it more or less ready. We developed it while working on some shiny app. I'll make it public next week so you can have a look. I think it should be better than d3Network.

gaborcsardi commented 9 years ago

@mbojan Sounds great! Better in what way? d3Network does not use igraph graphs AFAIK.

mbojan commented 9 years ago

The solution is underway. We are working on https://github.com/mbojan/d3net and still need to do some refactoring to be able to separate a function that will generate the SVG so that e.g. a D3 network picture could be embedded in a Rmd document (which would be nice!).

gaborcsardi commented 9 years ago

:+1: Cool!

timelyportfolio commented 9 years ago

Let me know if I can help making this into a an htmlwidget, or I could just start. Also, networkD3 is the new version of d3Network. I'm checking at d3net as I type.

mbojan commented 9 years ago

@gaborcsardi primarly we would like as many D3 attributes to be modifiable from R (layout, graphical attributes of vertices and edgses, and so on) as possible.

@timelyportfolio I have to say I still have to have a closer look at htmlwidgets. A lot of projects popped-up doing similar functionality.... Sounds good though.

mbojan commented 9 years ago

See this https://github.com/christophergandrud/networkD3 and this https://github.com/christophergandrud/networkD3/issues/49

gaborcsardi commented 9 years ago

Yeah, I know about that. If it'll have a igraph -> d3 JSON converter, then we can close this issue.

mbojan commented 9 years ago

So you'd rather actually bypass those two functions in networkD3? At this moment it is possible to extract edge list and some attributes from igraph objects and pass them to, say, networkD3::forceNetwork, isn't it.

gaborcsardi commented 9 years ago

I am not sure what you mean.

What I am trying to see, is that if this conversion is solved in networkD3, then we don't need to implement it here. That's all.

mbojan commented 9 years ago

As far as I can tell e.g. https://github.com/christophergandrud/networkD3/blob/master/R/forceNetwork.R accepts edge list (data.frame) plus some aux info on colors, weights etc. as vectors and that's it. No need for JSON.

gaborcsardi commented 9 years ago

That's great, but I'd still like to have a d3 json exporter. :)

mbojan commented 9 years ago

OK, that's what I was asking about in the first place :)

timelyportfolio commented 8 years ago

Where are we on this? I have done this in lots of places and code. Seems like the central source igraph is the best place to put it. I also wanted to reference https://github.com/jeroenooms/jsonlite/issues/113. It would be great to do both get and graph for import and export.

gaborcsardi commented 8 years ago

Nowhere, afaik. It is fairly easy, so I can do it quickly.....

timelyportfolio commented 8 years ago

Maybe this is a good start. jsonlite defaults in toJSON will create a d3 expected object.

library(igraph)

igraph_to_d3 <- function(igrf){
  igraph_df <- get.data.frame(igrf,what="both")

  names(igraph_df) <- c("nodes","links")

  # if vertices are empty then create a column id
  if(ncol(igraph_df$nodes)==0) {
    igraph_df$nodes$id <- unique(c(igraph_df$links$from,igraph_df$links$to))
  }

  # if vertices have rownames and no id column, make rownames = id
  if(!is.null(rownames(igraph_df$nodes)) && !("id" %in% colnames(igraph_df))){
    igraph_df$nodes$id <- rownames(igraph_df$nodes)
  }

  # subtract 1 for JavaScript 0 based indexing
  if(is.numeric(igraph_df$nodes$id)) {
    igraph_df$nodes$id <- igraph_df$nodes$id - 1
    igraph_df$links$from <- igraph_df$links$from - 1
    igraph_df$links$to <- igraph_df$links$to - 1
  }

  igraph_df
}

igraph_to_d3(graph.ring(10))

data(karate,package="igraphdata")
igraph_to_d3(karate)
timelyportfolio commented 8 years ago

Note however this is not the {name:..., children:[...]} format.

timelyportfolio commented 8 years ago

finally getting around to a more robust implementation (see https://github.com/timelyportfolio/d3r/issues/3)

ntamas commented 3 years ago

Is the conversion possible now via igraph -> tidygraph -> d3?

timelyportfolio commented 3 years ago

I think https://timelyportfolio.github.io/d3r/reference/d3_igraph.html is still the only option. Let me know if there are features that you would like to see.