Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

R: exporter to D3.js #17

Open
gaborcsardi opened this issue Jan 14, 2015 · 26 comments
Open

R: exporter to D3.js #17

gaborcsardi opened this issue Jan 14, 2015 · 26 comments
Labels
wishlist Feature request that has not been chosen for implementation yet; vote or comment to prioritize it!
Milestone

Comments

@gaborcsardi
Copy link
Contributor

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
Copy link
Contributor Author

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

@gaborcsardi
Copy link
Contributor Author

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
Copy link
Contributor Author

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
Copy link
Contributor Author

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
Copy link
Contributor Author

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
Copy link
Contributor Author

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
Copy link

mbojan commented Jan 14, 2015

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
Copy link
Contributor Author

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

@mbojan
Copy link

mbojan commented Feb 24, 2015

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
Copy link
Contributor Author

👍 Cool!

@timelyportfolio
Copy link

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
Copy link

mbojan commented Feb 24, 2015

@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
Copy link

mbojan commented Jun 22, 2015

@gaborcsardi
Copy link
Contributor Author

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

@mbojan
Copy link

mbojan commented Jun 22, 2015

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
Copy link
Contributor Author

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
Copy link

mbojan commented Jun 22, 2015

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
Copy link
Contributor Author

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

@mbojan
Copy link

mbojan commented Jun 22, 2015

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

@timelyportfolio
Copy link

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 jeroen/jsonlite#113. It would be great to do both get and graph for import and export.

@gaborcsardi
Copy link
Contributor Author

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

@timelyportfolio
Copy link

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
Copy link

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

@timelyportfolio
Copy link

finally getting around to a more robust implementation (see timelyportfolio/d3r#3)

@ntamas
Copy link
Member

ntamas commented Sep 29, 2021

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

@timelyportfolio
Copy link

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.

@ntamas ntamas added the wishlist Feature request that has not been chosen for implementation yet; vote or comment to prioritize it! label Oct 29, 2022
@krlmlr krlmlr added this to the future milestone Feb 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
wishlist Feature request that has not been chosen for implementation yet; vote or comment to prioritize it!
Projects
None yet
Development

No branches or pull requests

5 participants