rtidygraph

Error in tidygraph `as_tbl_graph` - mutliple logicals compared to single


I encountered an issue in the tidygraph package.

Here is an example code:

library(tidygraph)
adj <- structure(c(0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
                   0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 
                   0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 2, 0, 
                   0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 2, 2, 0, 0, 0, 0, 0, 1, 0, 0, 1, 
                   0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
                   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
                   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
                   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
                   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
                   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
                   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), 
                 dim = c(15L, 15L), 
                 dimnames = list(paste0("v",1:15), 
                                 paste0("v",1:15)))

adj

tidygraph::as_tbl_graph(adj)

The error that I get is:

Error in (is.null(rownames(x)) && is.null(colnames(x))) || colnames(x) ==  : 
  'length = 15' in coercion to 'logical(1)'

The error comes from the sub-function guess_matrix_type and there from the expression colnames(x) == rownames(x) which does not result in a single logical statement but multiple (that are then compared to a single one) here:

enter image description here

This used to work before (I think at least...). Any ideas on a workaround?


Solution

  • I can't replicate your error, but the obvious workaround is to use graph_from_adjacency_matrix from igraph

    igraph::graph_from_adjacency_matrix(adj) |>
      as_tbl_graph()
    # A tbl_graph: 15 nodes and 22 edges
    #
    # A directed acyclic multigraph with 1 component
    #
    # Node Data: 15 x 1 (active)
      name 
      <chr>
    1 v1   
    2 v2   
    3 v3   
    4 v4   
    5 v5   
    6 v6   
    # ... with 9 more rows
    #
    # Edge Data: 22 x 2
       from    to
      <int> <int>
    1     1     3
    2     2     6
    3     3     4
    # ... with 19 more rows