rigraph icon indicating copy to clipboard operation
rigraph copied to clipboard

R: exporter to D3.js

Open gaborcsardi opened this issue 10 years ago • 26 comments

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 avatar Jan 14 '15 20:01 gaborcsardi

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

gaborcsardi avatar Jan 14 '15 20:01 gaborcsardi

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 avatar Jan 14 '15 20:01 gaborcsardi

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 avatar Jan 14 '15 20:01 gaborcsardi

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 avatar Jan 14 '15 20:01 gaborcsardi

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 avatar Jan 14 '15 20:01 gaborcsardi

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...

gaborcsardi avatar Jan 14 '15 20:01 gaborcsardi

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.

mbojan avatar Jan 14 '15 21:01 mbojan

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

gaborcsardi avatar Jan 14 '15 21:01 gaborcsardi

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!).

mbojan avatar Feb 24 '15 14:02 mbojan

:+1: Cool!

gaborcsardi avatar Feb 24 '15 14:02 gaborcsardi

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.

timelyportfolio avatar Feb 24 '15 14:02 timelyportfolio

@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 avatar Feb 24 '15 15:02 mbojan

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

mbojan avatar Jun 22 '15 12:06 mbojan

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

gaborcsardi avatar Jun 22 '15 12:06 gaborcsardi

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.

mbojan avatar Jun 22 '15 13:06 mbojan

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.

gaborcsardi avatar Jun 22 '15 13:06 gaborcsardi

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.

mbojan avatar Jun 22 '15 13:06 mbojan

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

gaborcsardi avatar Jun 22 '15 13:06 gaborcsardi

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

mbojan avatar Jun 22 '15 13:06 mbojan

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.

timelyportfolio avatar Dec 31 '15 15:12 timelyportfolio

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

gaborcsardi avatar Dec 31 '15 15:12 gaborcsardi

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 avatar Dec 31 '15 16:12 timelyportfolio

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

timelyportfolio avatar Dec 31 '15 16:12 timelyportfolio

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

timelyportfolio avatar Aug 31 '16 15:08 timelyportfolio

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

ntamas avatar Sep 29 '21 10:09 ntamas

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.

timelyportfolio avatar Sep 29 '21 12:09 timelyportfolio