thomasp85 / tidygraph

A tidy API for graph manipulation
https://tidygraph.data-imaginist.com
Other
546 stars 61 forks source link

as_tbl_graph issue with 2x2 matrix #148

Closed annahyan closed 2 years ago

annahyan commented 2 years ago

Hi,

as_tbl_graph() throws and error when 2x2 matrix is piped into it, while it works with a 3x3 matrix. Please see the code below.

library(tidyverse)
library(tidygraph)
#> 
#> Attaching package: 'tidygraph'
#> The following object is masked from 'package:stats':
#> 
#>     filter

mat2 = matrix(0, nrow = 2, ncol = 2, dimnames = list(letters[1:2], letters[1:2]) )
mat2[1,2] = 1
mat2[2,1] = 1

mat2 
#>   a b
#> a 0 1
#> b 1 0

mat2 %>% as_tbl_graph(directed = FALSE)
#> Error in (function (edges, n = max(edges), directed = TRUE) : At structure_generators.c:86 : Invalid (negative) vertex id, Invalid vertex id

mat3 = matrix(0, nrow = 3, ncol = 3, dimnames = list(letters[1:3], letters[1:3]) )
mat3[1,2] = 1
mat3[2,1] = 1

mat3
#>   a b c
#> a 0 1 0
#> b 1 0 0
#> c 0 0 0

mat3 %>% as_tbl_graph(directed = FALSE)
#> # A tbl_graph: 3 nodes and 1 edges
#> #
#> # An unrooted forest with 2 trees
#> #
#> # Node Data: 3 × 1 (active)
#>   name 
#>   <chr>
#> 1 a    
#> 2 b    
#> 3 c    
#> #
#> # Edge Data: 1 × 3
#>    from    to weight
#>   <int> <int>  <dbl>
#> 1     1     2      1

Created on 2021-12-22 by the reprex package (v2.0.1)