Coder Social home page Coder Social logo

Comments (8)

michaellevy avatar michaellevy commented on May 18, 2024

Creating the network via igraph::graph_from_edgelist and the converting works as expected:

igraph::graph_from_edgelist(as.matrix(el)) %>%
  as_tbl_graph()

from tidygraph.

thomasp85 avatar thomasp85 commented on May 18, 2024

Try setting directed = TRUE in tbl_graph?

from tidygraph.

michaellevy avatar michaellevy commented on May 18, 2024

directed = TRUE is documented as the default. I confirmed it doesn't change the result.

from tidygraph.

thomasp85 avatar thomasp85 commented on May 18, 2024

Ok - this was a “From-my-phone” suggestion, sorry for leading you astray.

I’ll look into it

from tidygraph.

michaellevy avatar michaellevy commented on May 18, 2024

No worries. It's easy enough to create and convert the igraph object, so it's not blocking me. Just fyi.

from tidygraph.

ramorel avatar ramorel commented on May 18, 2024

Problem seems to occur in the as_graph_node_edge method when dealing with a tibble. Note first that when using a data frame, the problem does not occur (provided stringsAsfactors = F):

> el <- data.frame(from = c("b", "c"), to = c("a", "b"), stringsAsFactors = F)
> nodes <- data.frame(name = letters[1:3], stringsAsFactors = F)
> tbl_graph(nodes, el)
# A tbl_graph: 3 nodes and 2 edges
#
# A rooted tree
#
# Node Data: 3 x 1 (active)
  name 
  <chr>
1 a    
2 b    
3 c    
#
# Edge Data: 2 x 2
   from    to
  <int> <int>
1     2     1
2     3     2

The issue occurs in the as_graph_node_edge when the method evaluates whether the edgelist columns are of class character. Currently, the evaluation reads: if (is.character(edges[, 1])) and then proceeds to match the edgelist names to the node names. If edges is a tibble, this will always evaluate to false, since the subset edges[, 1] produces a one column tibble and not a vector of characters.

> el <- data.frame(from = c("b", "c"), to = c("a", "b"), stringsAsFactors = F)
> class(el[, 1])
[1] "character"
> el <- tibble(from = c("b", "c"), to = c("a", "b"))
> class(el[, 1])
[1] "tbl_df"     "tbl"        "data.frame"

As a result, the matching of the names between the edgelist and nodes list does not occur, since if (is.character(edges[, 1])) evaluates to false. If the evaluation uses double bracket notation-- if (is.character(edges[[1]])) -- it evaluates to true and the matching occurs.

> el <- tibble(from = c("b", "c"), to = c("a", "b"))
> class(el[[1]])
[1] "character"

Replacing the single bracket notation for double bracket notation in as_graph_node_edge solves the problem.

from tidygraph.

ramorel avatar ramorel commented on May 18, 2024

I have filed a pull request fixing the issue. This is my very first pull request. I hope I have done it well and the request is clear 😬. #53

from tidygraph.

thomasp85 avatar thomasp85 commented on May 18, 2024

Thanks @ramorel - much appreciated!

The PR was well explained and executed - Congrats on becoming a contributor 🎉

from tidygraph.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.